Skip to content

Commit 9454f36

Browse files
mingglivictor
authored and
victor
committed
BUG: maximum recursion error when replacing empty lists (pandas-dev#22083)
1 parent 33beed8 commit 9454f36

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ Reshaping
667667
- Bug in :meth:`DataFrame.replace` raises RecursionError when converting OutOfBounds ``datetime64[ns, tz]`` (:issue:`20380`)
668668
- :func:`pandas.core.groupby.GroupBy.rank` now raises a ``ValueError`` when an invalid value is passed for argument ``na_option`` (:issue:`22124`)
669669
- Bug in :func:`get_dummies` with Unicode attributes in Python 2 (:issue:`22084`)
670+
- Bug in :meth:`DataFrame.replace` raises ``RecursionError`` when replacing empty lists (:issue:`22083`)
670671
-
671672

672673
Build Changes

pandas/core/internals/blocks.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,9 @@ def copy(self, deep=True, mgr=None):
777777

778778
def replace(self, to_replace, value, inplace=False, filter=None,
779779
regex=False, convert=True, mgr=None):
780-
""" replace the to_replace value with value, possible to create new
780+
"""replace the to_replace value with value, possible to create new
781781
blocks here this is just a call to putmask. regex is not used here.
782-
It is used in ObjectBlocks. It is here for API
783-
compatibility.
782+
It is used in ObjectBlocks. It is here for API compatibility.
784783
"""
785784

786785
inplace = validate_bool_kwarg(inplace, 'inplace')
@@ -802,6 +801,11 @@ def replace(self, to_replace, value, inplace=False, filter=None,
802801
copy=not inplace) for b in blocks]
803802
return blocks
804803
except (TypeError, ValueError):
804+
# GH 22083, TypeError or ValueError occurred within error handling
805+
# causes infinite loop. Cast and retry only if not objectblock.
806+
if is_object_dtype(self):
807+
raise
808+
805809
# try again with a compatible block
806810
block = self.astype(object)
807811
return block.replace(to_replace=original_to_replace,

pandas/core/missing.py

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def mask_missing(arr, values_to_mask):
6868
else:
6969
mask |= isna(arr)
7070

71+
# GH 21977
72+
if mask is None:
73+
mask = np.zeros(arr.shape, dtype=bool)
74+
7175
return mask
7276

7377

pandas/tests/frame/test_replace.py

+14
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,20 @@ def test_replace_list(self):
603603

604604
assert_frame_equal(res, expec)
605605

606+
def test_replace_with_empty_list(self):
607+
# GH 21977
608+
s = pd.Series([['a', 'b'], [], np.nan, [1]])
609+
df = pd.DataFrame({'col': s})
610+
expected = df
611+
result = df.replace([], np.nan)
612+
assert_frame_equal(result, expected)
613+
614+
# GH 19266
615+
with tm.assert_raises_regex(ValueError, "cannot assign mismatch"):
616+
df.replace({np.nan: []})
617+
with tm.assert_raises_regex(ValueError, "cannot assign mismatch"):
618+
df.replace({np.nan: ['dummy', 'alt']})
619+
606620
def test_replace_series_dict(self):
607621
# from GH 3064
608622
df = DataFrame({'zero': {'a': 0.0, 'b': 1}, 'one': {'a': 2.0, 'b': 0}})

pandas/tests/series/test_replace.py

+13
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ def test_replace_with_single_list(self):
130130
s.replace([1, 2, 3], inplace=True, method='crash_cymbal')
131131
tm.assert_series_equal(s, ser)
132132

133+
def test_replace_with_empty_list(self):
134+
# GH 21977
135+
s = pd.Series([[1], [2, 3], [], np.nan, [4]])
136+
expected = s
137+
result = s.replace([], np.nan)
138+
tm.assert_series_equal(result, expected)
139+
140+
# GH 19266
141+
with tm.assert_raises_regex(ValueError, "cannot assign mismatch"):
142+
s.replace({np.nan: []})
143+
with tm.assert_raises_regex(ValueError, "cannot assign mismatch"):
144+
s.replace({np.nan: ['dummy', 'alt']})
145+
133146
def test_replace_mixed_types(self):
134147
s = pd.Series(np.arange(5), dtype='int64')
135148

0 commit comments

Comments
 (0)