-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fix bug in loc setitem changing the dtype when condition is False #37672
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 all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
9df9c89
Fix bug in setitem when indexer was only false changed dtype
phofl 6e0ec24
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl 26b7dae
Add space
phofl 11d27b1
Move condition to elif
phofl a527342
Move test
phofl 722cf9d
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl 345b3c1
Adjust test
phofl 8c57c09
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl 3b92d31
Fix whatsnew
phofl 97203de
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl 2f7bf13
Make test more clear
phofl eb3b204
Use asarray
phofl 9d59de4
Parametrize test
phofl b02e629
Move if condition
phofl b27c825
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl 8edc7d0
Remove import
phofl e57e407
Rename parametrization
phofl 6506df5
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl 8cdef72
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl c14d7b2
Move whatsnew
phofl 01d7ffc
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl da94459
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl 85120c9
Fix dep warning
phofl bae602e
Fix corner case
phofl 824c927
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl 6871ae9
Merge branch 'master' of https://github.com/pandas-dev/pandas into 37550
phofl 56f4f76
Merge remote-tracking branch 'upstream/master' into 37550
phofl d334c7a
Move check
phofl f12ef06
Split tests
phofl 131d168
Merge remote-tracking branch 'upstream/master' into 37550
phofl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -922,6 +922,33 @@ def test_setitem_boolean_mask(self, mask_type, float_frame): | |
expected.values[np.array(mask)] = np.nan | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.xfail(reason="Currently empty indexers are treated as all False") | ||
@pytest.mark.parametrize("box", [list, np.array, Series]) | ||
def test_setitem_loc_empty_indexer_raises_with_non_empty_value(self, box): | ||
# GH#37672 | ||
df = DataFrame({"a": ["a"], "b": [1], "c": [1]}) | ||
if box == Series: | ||
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. nitpick "box is Series", i can change this in my next CLN branch |
||
indexer = box([], dtype="object") | ||
else: | ||
indexer = box([]) | ||
msg = "Must have equal len keys and value when setting with an iterable" | ||
with pytest.raises(ValueError, match=msg): | ||
df.loc[indexer, ["b"]] = [1] | ||
|
||
@pytest.mark.parametrize("box", [list, np.array, Series]) | ||
def test_setitem_loc_only_false_indexer_dtype_changed(self, box): | ||
# GH#37550 | ||
# Dtype is only changed when value to set is a Series and indexer is | ||
# empty/bool all False | ||
df = DataFrame({"a": ["a"], "b": [1], "c": [1]}) | ||
indexer = box([False]) | ||
df.loc[indexer, ["b"]] = 10 - df["c"] | ||
expected = DataFrame({"a": ["a"], "b": [1], "c": [1]}) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tm.assert_frame_equal(df, expected) | ||
|
||
df.loc[indexer, ["b"]] = 9 | ||
tm.assert_frame_equal(df, expected) | ||
|
||
@pytest.mark.parametrize("indexer", [tm.setitem, tm.loc]) | ||
def test_setitem_boolean_mask_aligning(self, indexer): | ||
# GH#39931 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
evidently it works, but this seems like a weird place to handle this. is my intuition wrong here?
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.
This ensures, that an indexer like
(np.array([]), np.array([1]))
is handled the same as(np.array([]), 1)
, hence I handled it here.