Skip to content

BUG&TST: df.replace fail after converting to new dtype #31545

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 10 commits into from
Feb 1, 2020
8 changes: 8 additions & 0 deletions pandas/tests/frame/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,3 +1356,11 @@ def test_replace_replacer_dtype(self, replacer):
result = df.replace({"a": replacer, "b": replacer})
expected = pd.DataFrame([replacer])
tm.assert_frame_equal(result, expected)

def test_replace_after_convert_dtypes(self):
# GH31517
df = pd.DataFrame({"grp": [1, 2, 3, 4, 5]})
Copy link
Member

Choose a reason for hiding this comment

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

I would just specify it here as dtype="Int64" instead of using convert_dtypes (in the end, the issue was that replace didn't work with Int64 data, the convert_dtypes was just a way to get such dtype)

Copy link
Member Author

Choose a reason for hiding this comment

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

oh yeah, i was copying-pasting the example in issue description, sorry for my laziness. 😅 changed!

df = df.convert_dtypes()
result = df.replace(1, 10)
expected = pd.DataFrame({"grp": [10, 2, 3, 4, 5]}, dtype="Int64")
tm.assert_frame_equal(result, expected)