Skip to content

TST:add test for df replace GH34871 #34904

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
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,3 +1403,17 @@ def test_replace_with_duplicate_columns(self, replacement):
result["B"] = result["B"].replace(7, replacement)

tm.assert_frame_equal(result, expected)

def test_replace_period_ignore_float(self):
"""
Regression test for GH#34871: if df.replace(1.0, 0.0) is called on a df
with a Period column the old, faulty behavior is to raise TypeError.
"""
df = pd.DataFrame({"Per": [pd.Period("2020-01")] * 3})
df_after_replace = df.replace(1.0, 0.0)

df_expected_result = pd.DataFrame({"Per": [pd.Period("2020-01")] * 3})
# currently replace() changes dtype from Period to object
df_expected_result["Per"] = df_expected_result["Per"].astype(object)
Copy link
Contributor

Choose a reason for hiding this comment

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

so this is wrong, we want to xfail this test, this should not change the dtype, pls add the issue number in the xfail itself

Copy link
Contributor

Choose a reason for hiding this comment

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

meaning, yes it doesn't raise an expection, but the output is still converted to 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.

gotcha, thanks for teaching me about xfail. i changed the test to now expect the result we would like to see from replace() in the future, and to xfail for now.


tm.assert_frame_equal(df_expected_result, df_after_replace)