Skip to content

Commit 13687c6

Browse files
committed
BUG: RecursionError when attempting to replace np.nan values (pandas-dev#45725)
1 parent a966f77 commit 13687c6

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ Other Deprecations
193193
- Deprecated :meth:`DataFrame.iteritems`, :meth:`Series.iteritems`, :meth:`HDFStore.iteritems` in favor of :meth:`DataFrame.items`, :meth:`Series.items`, :meth:`HDFStore.items` (:issue:`45321`)
194194
- Deprecated :meth:`Series.is_monotonic` and :meth:`Index.is_monotonic` in favor of :meth:`Series.is_monotonic_increasing` and :meth:`Index.is_monotonic_increasing` (:issue:`45422`, :issue:`21335`)
195195
- Deprecated the ``__array_wrap__`` method of DataFrame and Series, rely on standard numpy ufuncs instead (:issue:`45451`)
196+
- Deprecated the behavior of :meth:`Series.fillna` and :meth:`DataFrame.fillna` with ``timedelta64[ns]`` dtype and incompatible fill value; in a future version this will cast to a common dtype (usually object) instead of raising, matching the behavior of other dtypes (:issue:`45746`)
196197
-
197198

198199

@@ -248,7 +249,6 @@ Conversion
248249
- Bug in :meth:`Series.astype` and :meth:`DataFrame.astype` from floating dtype to unsigned integer dtype failing to raise in the presence of negative values (:issue:`45151`)
249250
- Bug in :func:`array` with ``FloatingDtype`` and values containing float-castable strings incorrectly raising (:issue:`45424`)
250251
- Bug when comparing string and datetime64ns objects causing ``OverflowError`` exception. (:issue:`45506`)
251-
- Bug when attempting to replace ``numpy.nan`` values causing ``RecursionError``
252252

253253
Strings
254254
^^^^^^^

pandas/tests/frame/methods/test_replace.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -661,12 +661,19 @@ def test_replace_simple_nested_dict_with_nonexistent_value(self):
661661
result = df.replace({"col": {-1: "-", 1: "a", 4: "b"}})
662662
tm.assert_frame_equal(expected, result)
663663

664-
@pytest.mark.parametrize("to_replace, value", [(np.nan, pd.NA), (np.nan, None)])
665-
def test_replace_numpy_nan(self, to_replace, value):
666-
# GH#45725 ensure numpy.nan can be replaced with pandas.NA or None
667-
df = DataFrame({"A": [to_replace]}, dtype=object)
668-
result = df.replace({to_replace: value})
669-
expected = DataFrame({"A": [value]}, dtype=object)
664+
def test_replace_numpy_nan(self, nulls_fixture):
665+
# GH#45725 ensure numpy.nan can be replaced with all other null types
666+
to_replace = np.nan
667+
value = nulls_fixture
668+
dtype = object
669+
df = DataFrame({"A": [to_replace]}, dtype=dtype)
670+
expected = DataFrame({"A": [value]}, dtype=dtype)
671+
672+
result = df.replace({to_replace: value}).astype(dtype=dtype)
673+
tm.assert_frame_equal(result, expected)
674+
675+
# same thing but different calling convention
676+
result = df.replace(to_replace, value).astype(dtype=dtype)
670677
tm.assert_frame_equal(result, expected)
671678

672679
def test_replace_value_is_none(self, datetime_frame):

pandas/tests/series/methods/test_replace.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,22 @@ def test_replace_explicit_none(self):
3636
assert expected.iloc[-1] is None
3737
tm.assert_series_equal(result, expected)
3838

39-
@pytest.mark.parametrize("to_replace, value", [(np.nan, pd.NA), (np.nan, None)])
40-
def test_replace_numpy_nan(self, to_replace, value):
41-
# GH#45725 ensure numpy.nan can be replaced with pandas.NA or None
42-
ser = pd.Series([to_replace], dtype=object)
43-
result = ser.replace({to_replace: value})
44-
expected = pd.Series([value], dtype=object)
39+
def test_replace_numpy_nan(self, nulls_fixture):
40+
# GH#45725 ensure numpy.nan can be replaced with all other null types
41+
to_replace = np.nan
42+
value = nulls_fixture
43+
dtype = object
44+
ser = pd.Series([to_replace], dtype=dtype)
45+
expected = pd.Series([value], dtype=dtype)
46+
47+
result = ser.replace({to_replace: value}).astype(dtype=dtype)
4548
tm.assert_series_equal(result, expected)
46-
assert result.dtype == object
49+
assert result.dtype == dtype
50+
51+
# same thing but different calling convention
52+
result = ser.replace(to_replace, value).astype(dtype=dtype)
53+
tm.assert_series_equal(result, expected)
54+
assert result.dtype == dtype
4755

4856
def test_replace_noop_doesnt_downcast(self):
4957
# GH#44498

0 commit comments

Comments
 (0)