Skip to content

Commit c8f4f72

Browse files
committed
only retry if the block is not ObjectBlock
1 parent f657ae3 commit c8f4f72

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

pandas/core/internals/blocks.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -801,12 +801,17 @@ def replace(self, to_replace, value, inplace=False, filter=None,
801801
copy=not inplace) for b in blocks]
802802
return blocks
803803
except (TypeError, ValueError):
804-
805-
# try again with a compatible block
806-
block = self.astype(object)
807-
return block.replace(
808-
to_replace=original_to_replace, value=value, inplace=inplace,
809-
filter=filter, regex=regex, convert=convert)
804+
if self.dtype == 'object':
805+
raise
806+
else:
807+
# try again with a compatible block
808+
block = self.astype(object)
809+
return block.replace(to_replace=original_to_replace,
810+
value=value,
811+
inplace=inplace,
812+
filter=filter,
813+
regex=regex,
814+
convert=convert)
810815

811816
def _replace_single(self, *args, **kwargs):
812817
""" no-op on a non-ObjectBlock """
@@ -992,16 +997,15 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0,
992997
#
993998
# TODO: this prob needs some better checking
994999
# for 2D cases
995-
if ((is_list_like(new) and np.any(mask[mask]) and
1000+
if ((is_list_like(new) and
1001+
np.any(mask[mask]) and
9961002
getattr(new, 'ndim', 1) == 1)):
1003+
9971004
if not (mask.shape[-1] == len(new) or
9981005
mask[mask].shape[-1] == len(new) or
9991006
len(new) == 1):
1000-
# GH 19266 and GH 21977
1001-
# ValueError triggers try except block in Block.replace
1002-
# causing RecursionError
1003-
raise Exception("cannot assign mismatch length "
1004-
"to masked array")
1007+
raise ValueError("cannot assign mismatch "
1008+
"length to masked array")
10051009

10061010
np.putmask(new_values, mask, new)
10071011

0 commit comments

Comments
 (0)