Skip to content

Commit

Permalink
BUG: RecursionError when attempting to replace "np.nan" values under…
Browse files Browse the repository at this point in the history
… main branch (pandas-dev#45725)
  • Loading branch information
roib20 committed Feb 1, 2022
1 parent 2ff7dc0 commit f2ff20d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ def replace(
# go through replace_list

values = self.values
value = self._standardize_fill_value(value) # GH#45725

if isinstance(values, Categorical):
# TODO: avoid special-casing
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,18 @@ def test_replace_simple_nested_dict_with_nonexistent_value(self):
result = df.replace({"col": {-1: "-", 1: "a", 4: "b"}})
tm.assert_frame_equal(expected, result)

def test_replace_numpy_nan(self):
# GH#45725 ensure np.nan can be replaced with pd.NA
df = pd.DataFrame({"A": [np.nan], "B": [np.nan]}, dtype=object)
result = df.replace({np.nan: pd.NA})
expected = pd.DataFrame({"A": [pd.NA], "B": [pd.NA]}, dtype=object)
tm.assert_frame_equal(result, expected)

# same thing but with None
result = df.replace({np.nan: None})
expected = pd.DataFrame({"A": [None], "B": [None]}, dtype=object)
tm.assert_frame_equal(result, expected)

def test_replace_value_is_none(self, datetime_frame):
orig_value = datetime_frame.iloc[0, 0]
orig2 = datetime_frame.iloc[1, 0]
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ def test_replace_explicit_none(self):
assert expected.iloc[-1] is None
tm.assert_series_equal(result, expected)

def test_replace_numpy_nan(self):
# GH#45725 ensure np.nan can be replaced
ser = pd.Series([np.nan, np.nan], dtype=object)
result = ser.replace({np.nan: pd.NA})
expected = pd.Series([pd.NA, pd.NA], dtype=object)
tm.assert_series_equal(result, expected)
assert result.dtype == object

# same thing but with None
result = ser.replace({np.nan: None})
expected = pd.Series([None, None], dtype=object)
tm.assert_series_equal(result, expected)
assert result.dtype == object

def test_replace_noop_doesnt_downcast(self):
# GH#44498
ser = pd.Series([None, None, pd.Timestamp("2021-12-16 17:31")], dtype=object)
Expand Down

0 comments on commit f2ff20d

Please sign in to comment.