Skip to content

Commit ee984c1

Browse files
authored
CLN: use putmask_simple instead of putmask (#37860)
1 parent d45903e commit ee984c1

File tree

1 file changed

+12
-36
lines changed

1 file changed

+12
-36
lines changed

pandas/core/internals/blocks.py

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
TD64NS_DTYPE,
3333
is_bool_dtype,
3434
is_categorical_dtype,
35+
is_datetime64_any_dtype,
3536
is_datetime64_dtype,
3637
is_datetime64tz_dtype,
3738
is_dtype_equal,
@@ -791,10 +792,9 @@ def replace(
791792
regex=regex,
792793
)
793794

794-
blocks = self.putmask(mask, value, inplace=inplace)
795-
blocks = extend_blocks(
796-
[b.convert(numeric=False, copy=not inplace) for b in blocks]
797-
)
795+
blk = self if inplace else self.copy()
796+
blk._putmask_simple(mask, value)
797+
blocks = blk.convert(numeric=False, copy=not inplace)
798798
return blocks
799799

800800
def _replace_regex(
@@ -1184,39 +1184,15 @@ def coerce_to_target_dtype(self, other):
11841184
# don't coerce float/complex to int
11851185
return self
11861186

1187-
elif (
1188-
self.is_datetime
1189-
or is_datetime64_dtype(dtype)
1190-
or is_datetime64tz_dtype(dtype)
1191-
):
1192-
1193-
# not a datetime
1194-
if not (
1195-
(is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype))
1196-
and self.is_datetime
1197-
):
1198-
return self.astype(object)
1199-
1200-
# don't upcast timezone with different timezone or no timezone
1201-
mytz = getattr(self.dtype, "tz", None)
1202-
othertz = getattr(dtype, "tz", None)
1203-
1204-
if not tz_compare(mytz, othertz):
1205-
return self.astype(object)
1206-
1207-
raise AssertionError(
1208-
f"possible recursion in coerce_to_target_dtype: {self} {other}"
1209-
)
1187+
elif self.is_datetime or is_datetime64_any_dtype(dtype):
1188+
# The is_dtype_equal check above ensures that at most one of
1189+
# these two conditions hold, so we must cast to object.
1190+
return self.astype(object)
12101191

12111192
elif self.is_timedelta or is_timedelta64_dtype(dtype):
1212-
1213-
# not a timedelta
1214-
if not (is_timedelta64_dtype(dtype) and self.is_timedelta):
1215-
return self.astype(object)
1216-
1217-
raise AssertionError(
1218-
f"possible recursion in coerce_to_target_dtype: {self} {other}"
1219-
)
1193+
# The is_dtype_equal check above ensures that at most one of
1194+
# these two conditions hold, so we must cast to object.
1195+
return self.astype(object)
12201196

12211197
try:
12221198
return self.astype(dtype)
@@ -2588,7 +2564,7 @@ def _replace_list(
25882564
regex: bool = False,
25892565
) -> List["Block"]:
25902566
if len(algos.unique(dest_list)) == 1:
2591-
# We got likely here by tiling value inside NDFrame.replace,
2567+
# We likely got here by tiling value inside NDFrame.replace,
25922568
# so un-tile here
25932569
return self.replace(src_list, dest_list[0], inplace, regex)
25942570
return super()._replace_list(src_list, dest_list, inplace, regex)

0 commit comments

Comments
 (0)