Skip to content

Commit 01d0a08

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

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

pandas/tests/frame/methods/test_replace.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -661,16 +661,13 @@ 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-
def test_replace_numpy_nan(self):
665-
# GH#45725 ensure np.nan can be replaced with pd.NA
666-
df = pd.DataFrame({"A": [np.nan], "B": [np.nan]}, dtype=object)
667-
result = df.replace({np.nan: pd.NA})
668-
expected = pd.DataFrame({"A": [pd.NA], "B": [pd.NA]}, dtype=object)
669-
tm.assert_frame_equal(result, expected)
670-
671-
# same thing but with None
672-
result = df.replace({np.nan: None})
673-
expected = pd.DataFrame({"A": [None], "B": [None]}, dtype=object)
664+
@pytest.mark.parametrize("dtype", [object])
665+
@pytest.mark.parametrize("to_replace, value", [(np.nan, pd.NA), (np.nan, None)])
666+
def test_replace_numpy_nan(self, dtype, to_replace, value):
667+
# GH#45725 ensure numpy.nan can be replaced with pandas.NA or None
668+
df = pd.DataFrame({"A": [to_replace]}, dtype=dtype)
669+
result = df.replace({to_replace: value})
670+
expected = pd.DataFrame({"A": [value]}, dtype=dtype)
674671
tm.assert_frame_equal(result, expected)
675672

676673
def test_replace_value_is_none(self, datetime_frame):

pandas/tests/series/methods/test_replace.py

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

39-
def test_replace_numpy_nan(self):
40-
# GH#45725 ensure np.nan can be replaced
41-
ser = pd.Series([np.nan, np.nan], dtype=object)
42-
result = ser.replace({np.nan: pd.NA})
43-
expected = pd.Series([pd.NA, pd.NA], dtype=object)
44-
tm.assert_series_equal(result, expected)
45-
assert result.dtype == object
46-
47-
# same thing but with None
48-
result = ser.replace({np.nan: None})
49-
expected = pd.Series([None, None], dtype=object)
39+
@pytest.mark.parametrize("dtype", [object])
40+
@pytest.mark.parametrize("to_replace, value", [(np.nan, pd.NA), (np.nan, None)])
41+
def test_replace_numpy_nan(self, dtype, to_replace, value):
42+
# GH#45725 ensure numpy.nan can be replaced with pandas.NA or None
43+
ser = pd.Series([to_replace], dtype=dtype)
44+
result = ser.replace({to_replace: value})
45+
expected = pd.Series([value], dtype=dtype)
5046
tm.assert_series_equal(result, expected)
5147
assert result.dtype == object
5248

0 commit comments

Comments
 (0)