Skip to content

API: Series[bool][key] = np.nan -> cast to object #38709

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 23 commits into from
Jan 28, 2021
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
6ee5c5f
TST: implement tm.check_setitem_equivalents
jbrockmendel Dec 24, 2020
33e39f9
re-write as fixturized
jbrockmendel Dec 26, 2020
5896615
CLN: algos.searchsorted (#38686)
jbrockmendel Dec 24, 2020
90a76ce
REF: simplify coerce_to_target_dtype (#38683)
jbrockmendel Dec 24, 2020
0827cd0
PERF: fix assert_frame_equal can be very slow (#38202)
ivanovmg Dec 24, 2020
803f495
API: cast to object when setting np.nan into Series[bool]
jbrockmendel Dec 27, 2020
055617b
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 27, 2020
3be818f
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 28, 2020
51ad10d
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Dec 29, 2020
279564b
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 2, 2021
e3b2cec
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 4, 2021
c4eb8e1
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 6, 2021
ce4311a
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 10, 2021
9e5cb27
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 16, 2021
c01661d
fix putmask
jbrockmendel Jan 17, 2021
23c69dc
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 17, 2021
198768e
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 18, 2021
7c6fdb7
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 19, 2021
e0c0194
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 20, 2021
f02f514
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 25, 2021
0934ad1
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 27, 2021
d966441
Merge branch 'master' of http://github.com/pandas-dev/pandas into bu…
jbrockmendel Jan 27, 2021
d2ac2b3
whatsnew, test, maybe_promote->find_common_type
jbrockmendel Jan 27, 2021
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
Prev Previous commit
Next Next commit
REF: simplify coerce_to_target_dtype (#38683)
  • Loading branch information
jbrockmendel committed Dec 26, 2020
commit 90a76cec2d80d52d94f647db4cec03977b762ef2
33 changes: 3 additions & 30 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,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 @@ -53,7 +51,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 @@ -927,7 +924,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 @@ -1164,33 +1161,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