|
32 | 32 | TD64NS_DTYPE,
|
33 | 33 | is_bool_dtype,
|
34 | 34 | is_categorical_dtype,
|
| 35 | + is_datetime64_any_dtype, |
35 | 36 | is_datetime64_dtype,
|
36 | 37 | is_datetime64tz_dtype,
|
37 | 38 | is_dtype_equal,
|
@@ -791,10 +792,9 @@ def replace(
|
791 | 792 | regex=regex,
|
792 | 793 | )
|
793 | 794 |
|
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) |
798 | 798 | return blocks
|
799 | 799 |
|
800 | 800 | def _replace_regex(
|
@@ -1184,39 +1184,15 @@ def coerce_to_target_dtype(self, other):
|
1184 | 1184 | # don't coerce float/complex to int
|
1185 | 1185 | return self
|
1186 | 1186 |
|
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) |
1210 | 1191 |
|
1211 | 1192 | 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) |
1220 | 1196 |
|
1221 | 1197 | try:
|
1222 | 1198 | return self.astype(dtype)
|
@@ -2588,7 +2564,7 @@ def _replace_list(
|
2588 | 2564 | regex: bool = False,
|
2589 | 2565 | ) -> List["Block"]:
|
2590 | 2566 | 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, |
2592 | 2568 | # so un-tile here
|
2593 | 2569 | return self.replace(src_list, dest_list[0], inplace, regex)
|
2594 | 2570 | return super()._replace_list(src_list, dest_list, inplace, regex)
|
|
0 commit comments