Skip to content
Merged
15 changes: 15 additions & 0 deletions pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,18 @@ def test_replace_value_none_dtype_numeric(self, val):
result = ser.replace(val, None)
expected = pd.Series([1, None], dtype=object)
tm.assert_series_equal(result, expected)

def test_replace_change_dtype_series(self):
# GH#25797
df = pd.DataFrame.from_dict({"Test": ["0.5", True, "0.6"]})
df["Test"] = df["Test"].replace([True], [np.nan])
expected = pd.DataFrame.from_dict({"Test": ["0.5", np.nan, "0.6"]})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last comment: Could you define expected only once after line 681 and reuse it for all assert_frame_equal statements? It's not necessary to define it multiple times when it's the same object :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right! How did I miss that one! Thanks again ;)

tm.assert_frame_equal(df, expected)

df = pd.DataFrame.from_dict({"Test": ["0.5", None, "0.6"]})
df["Test"] = df["Test"].replace([None], [np.nan])
tm.assert_frame_equal(df, expected)

df = pd.DataFrame.from_dict({"Test": ["0.5", None, "0.6"]})
df["Test"] = df["Test"].fillna(np.nan)
tm.assert_frame_equal(df, expected)