Skip to content

TST: ro_replace changing dtype of series #50664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 12, 2023
23 changes: 23 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,26 @@ 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"]})
print(df["Test"].dtype)
Copy link
Member

Choose a reason for hiding this comment

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

Some pointers: Please remove the print statements.

Generally, you can remove your first assert statements per block, but we want to compare the whole df with the second assert statement, e.g. define your expected object and then call assert_frame_equal

expected = ...
tm.assert_frame_equal(df, expected)

if expected is the same as your initial df, you can define expected through

expected = df.copy()

in the beginning

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Made the changes.

Correct me if I am wrong, but here the assert needs to check if the series dtype remains 'object' and not if the dataframes are equal. I've made changes accordingly.

Copy link
Member

Choose a reason for hiding this comment

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

tm.assert_frame_equal raises if your dtypes are different. So this is covered as well. In general, we want to check that the DataFrame is as expected (values, columns, index, dtype, ...). That is why we are using assert_frame_equal

The functions could modify the values unexpectedly and the test would not fail, if you only check the dtypes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah okay! Thanks for explaining :)

assert df["Test"].dtype == "object"
df["Test"] = df["Test"].replace([True], [np.nan])
print(df["Test"].dtype)
assert df["Test"].dtype == "object"

df = pd.DataFrame.from_dict({"Test": ["0.5", None, "0.6"]})
print(df["Test"].dtype)
assert df["Test"].dtype == "object"
df["Test"] = df["Test"].replace([None], [np.nan])
print(df["Test"].dtype)
assert df["Test"].dtype == "object"

df = pd.DataFrame.from_dict({"Test": ["0.5", None, "0.6"]})
print(df["Test"].dtype)
assert df["Test"].dtype == "object"
df["Test"] = df["Test"].fillna(np.nan)
print(df["Test"].dtype)
assert df["Test"].dtype == "object"