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 2 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
25 changes: 25 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,28 @@ 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
(reported for Pandas 1.0.4). The intended behavior is to just ignore
that column. This has been fixed in newer versions. This is a regression
test that triggers for the old, broken behavior, i.e.
TypeError is raised
"""
df = pd.DataFrame({"Per": [pd.Period("2020-01")] * 3})

# buggy behavior is to raise:
# TypeError: 'value' should be a 'Period', 'NaT', or array of those.
# Got 'float' instead
# so for now we want to simply call this and no exception should be
# raised, improving upon 1.0.4 behavior
# df_after_replace = df.replace(1.0, 0.0)
df.replace(1.0, 0.0)

# 'replace()' should be ignored for these inputs,
# so new df should equal old df
# this is currently still buggy, Period column becomes Object instead
# so needs fix in future. added this info to GH ticket
# tm.assert_frame_equal(df, df_after_replace)