Skip to content

Commit 65fe412

Browse files
Revert "CLN: short-circuit case in Block.replace (pandas-dev#27768)"
This reverts commit 01f90c1.
1 parent a84bfc0 commit 65fe412

File tree

1 file changed

+2
-34
lines changed

1 file changed

+2
-34
lines changed

pandas/core/internals/blocks.py

+2-34
Original file line numberDiff line numberDiff line change
@@ -684,26 +684,8 @@ def replace(
684684
# If we cannot replace with own dtype, convert to ObjectBlock and
685685
# retry
686686
if not self._can_hold_element(to_replace):
687-
if not isinstance(to_replace, list):
688-
if inplace:
689-
return [self]
690-
return [self.copy()]
691-
692-
to_replace = [x for x in to_replace if self._can_hold_element(x)]
693-
if not len(to_replace):
694-
# GH#28084 avoid costly checks since we can infer
695-
# that there is nothing to replace in this block
696-
if inplace:
697-
return [self]
698-
return [self.copy()]
699-
700-
if len(to_replace) == 1:
701-
# _can_hold_element checks have reduced this back to the
702-
# scalar case and we can avoid a costly object cast
703-
return self.replace(
704-
to_replace[0], value, inplace=inplace, regex=regex, convert=convert,
705-
)
706-
687+
# TODO: we should be able to infer at this point that there is
688+
# nothing to replace
707689
# GH 22083, TypeError or ValueError occurred within error handling
708690
# causes infinite loop. Cast and retry only if not objectblock.
709691
if is_object_dtype(self):
@@ -728,27 +710,14 @@ def replace(
728710

729711
mask = missing.mask_missing(values, to_replace)
730712

731-
if not mask.any():
732-
if inplace:
733-
return [self]
734-
return [self.copy()]
735-
736713
try:
737714
blocks = self.putmask(mask, value, inplace=inplace)
738-
# Note: it is _not_ the case that self._can_hold_element(value)
739-
# is always true at this point. In particular, that can fail
740-
# for:
741-
# "2u" with bool-dtype, float-dtype
742-
# 0.5 with int64-dtype
743-
# np.nan with int64-dtype
744715
except (TypeError, ValueError):
745716
# GH 22083, TypeError or ValueError occurred within error handling
746717
# causes infinite loop. Cast and retry only if not objectblock.
747718
if is_object_dtype(self):
748719
raise
749720

750-
assert not self._can_hold_element(value), value
751-
752721
# try again with a compatible block
753722
block = self.astype(object)
754723
return block.replace(
@@ -905,7 +874,6 @@ def putmask(
905874

906875
# if we are passed a scalar None, convert it here
907876
if not is_list_like(new) and isna(new) and not self.is_object:
908-
# FIXME: make sure we have compatible NA
909877
new = self.fill_value
910878

911879
if self._can_hold_element(new):

0 commit comments

Comments
 (0)