-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 4 commits
7e461a1
1314059
8bcb313
4f4f293
7529084
2c6d5f6
eb6cee8
18bd517
20ae4f4
2c97495
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -948,3 +948,15 @@ 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}}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you were to switch the replacement to In [2]: df = pd.DataFrame({'col': [False, True, 0, 1]})
In [3]: df.replace({'col': {False: 1, True: 0}})
Out[3]:
col
0 1
1 0
2 1
3 0 I think the current version of the test is passing because 0/1 are just getting replaced by themselves. This is a consequence of how Python handles hashing, specifically There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ehh, you are very right, it's not good solution @jschendel |
||
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"}}) |
There was a problem hiding this comment.
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).There was a problem hiding this comment.
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
There was a problem hiding this comment.
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