Skip to content

Replace with nested dict raises for overlapping keys #27696

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
Aug 27, 2019
6 changes: 5 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6640,7 +6640,11 @@ def replace(

for k, v in items:
keys, values = list(zip(*v.items())) or ([], [])
if set(keys) & set(values):
# add another check to avoid boolean being regarded
# as binary in python set
if set(keys) & set(values) and set(map(str, keys)) & set(
map(str, values)
Copy link
Member

Choose a reason for hiding this comment

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

I think this might be a little bit too permissive now, as it will allow {0: 1.0, 1: 'a'}, which was previously rejected (might not actually matter but is a change in behavior we should be cognizant of).

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, i should have thought of it more thoroughly

Copy link
Member

Choose a reason for hiding this comment

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

Can this just not be removed altogether? Not clear on the purpose of it

):
raise ValueError(
"Replacement not allowed with "
"overlapping keys and values"
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,3 +948,18 @@ def test_deprecated_get_dtype_counts(self):
df = DataFrame([1])
with tm.assert_produces_warning(FutureWarning):
df.get_dtype_counts()

def test_boolean_in_replace(self):
# GH 27660
df = DataFrame({"col": [False, True, 0, 1]})

# result = df.replace({"col": {False: 0, True: 1}})
# expected = DataFrame({"col": [0, 1, 0, 1]})
# assert_frame_equal(result, expected)

# msg = "Replacement not allowed with overlapping keys and values"
# with pytest.raises(ValueError, match=msg):
# df.replace({"col": {0: 1, 1: "a"}})

dd = df.replace({False:1, True:0})
assert dd['col'].values == 0