Skip to content

REF: simplify coerce_to_target_dtype #38683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 3 additions & 30 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
from pandas.core.dtypes.common import (
DT64NS_DTYPE,
TD64NS_DTYPE,
is_bool_dtype,
is_categorical_dtype,
is_datetime64_any_dtype,
is_datetime64_dtype,
is_datetime64tz_dtype,
is_dtype_equal,
Expand All @@ -55,7 +53,6 @@
is_re,
is_re_compilable,
is_sparse,
is_timedelta64_dtype,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import CategoricalDtype, ExtensionDtype
Expand Down Expand Up @@ -924,7 +921,7 @@ def setitem(self, indexer, value):

else:
# current dtype cannot store value, coerce to common dtype

# TODO: can we just use coerce_to_target_dtype for all this
if hasattr(value, "dtype"):
dtype = value.dtype

Expand Down Expand Up @@ -1167,33 +1164,9 @@ def coerce_to_target_dtype(self, other):
# if we cannot then coerce to object
dtype, _ = infer_dtype_from(other, pandas_dtype=True)

if is_dtype_equal(self.dtype, dtype):
return self

if self.is_bool or is_object_dtype(dtype) or is_bool_dtype(dtype):
# we don't upcast to bool
return self.astype(object)

elif (self.is_float or self.is_complex) and (
is_integer_dtype(dtype) or is_float_dtype(dtype)
):
# don't coerce float/complex to int
return self
new_dtype = find_common_type([self.dtype, dtype])

elif self.is_datetime or is_datetime64_any_dtype(dtype):
# The is_dtype_equal check above ensures that at most one of
# these two conditions hold, so we must cast to object.
return self.astype(object)

elif self.is_timedelta or is_timedelta64_dtype(dtype):
# The is_dtype_equal check above ensures that at most one of
# these two conditions hold, so we must cast to object.
return self.astype(object)

try:
return self.astype(dtype)
except (ValueError, TypeError, OverflowError):
return self.astype(object)
return self.astype(new_dtype, copy=False)

def interpolate(
self,
Expand Down