@@ -684,26 +684,8 @@ def replace(
684
684
# If we cannot replace with own dtype, convert to ObjectBlock and
685
685
# retry
686
686
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
707
689
# GH 22083, TypeError or ValueError occurred within error handling
708
690
# causes infinite loop. Cast and retry only if not objectblock.
709
691
if is_object_dtype (self ):
@@ -728,27 +710,14 @@ def replace(
728
710
729
711
mask = missing .mask_missing (values , to_replace )
730
712
731
- if not mask .any ():
732
- if inplace :
733
- return [self ]
734
- return [self .copy ()]
735
-
736
713
try :
737
714
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
744
715
except (TypeError , ValueError ):
745
716
# GH 22083, TypeError or ValueError occurred within error handling
746
717
# causes infinite loop. Cast and retry only if not objectblock.
747
718
if is_object_dtype (self ):
748
719
raise
749
720
750
- assert not self ._can_hold_element (value ), value
751
-
752
721
# try again with a compatible block
753
722
block = self .astype (object )
754
723
return block .replace (
@@ -905,7 +874,6 @@ def putmask(
905
874
906
875
# if we are passed a scalar None, convert it here
907
876
if not is_list_like (new ) and isna (new ) and not self .is_object :
908
- # FIXME: make sure we have compatible NA
909
877
new = self .fill_value
910
878
911
879
if self ._can_hold_element (new ):
0 commit comments