-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
ENH: Support Series[bool]
as indexer for iloc.__getitem__
#61162
Merged
mroeschke
merged 11 commits into
pandas-dev:main
from
arthurlw:pass-boolean-to-iloc.__getitem__
Apr 9, 2025
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
69468cd
updated indexing.py to allow iloc.__getitem__
arthurlw ded44cb
Updated test_iloc_mask test
arthurlw b4d58e1
bugfix test_iloc_mask test
arthurlw 326b91c
bugfix test_iloc_mask
arthurlw 2c8174c
whatsnew
arthurlw 9345465
added test to test_iloc_mask
arthurlw 7533f64
formatting
arthurlw 35bf005
precommit
arthurlw 6780260
added tests for series bool mask
arthurlw 1c92fc8
precommit
arthurlw 2481063
reformatted tests
arthurlw 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 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 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 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 |
---|---|---|
|
@@ -726,15 +726,16 @@ def test_iloc_setitem_with_scalar_index(self, indexer, value): | |
|
||
@pytest.mark.filterwarnings("ignore::UserWarning") | ||
def test_iloc_mask(self): | ||
# GH 3631, iloc with a mask (of a series) should raise | ||
# GH 60994, iloc with a mask (of a series) should return accordingly | ||
df = DataFrame(list(range(5)), index=list("ABCDE"), columns=["a"]) | ||
mask = df.a % 2 == 0 | ||
msg = "iLocation based boolean indexing cannot use an indexable as a mask" | ||
with pytest.raises(ValueError, match=msg): | ||
df.iloc[mask] | ||
|
||
mask.index = range(len(mask)) | ||
msg = "iLocation based boolean indexing on an integer type is not available" | ||
with pytest.raises(NotImplementedError, match=msg): | ||
msg = "Unalignable boolean Series provided as indexer" | ||
with pytest.raises(IndexingError, match=msg): | ||
df.iloc[mask] | ||
|
||
# ndarray ok | ||
|
@@ -753,25 +754,20 @@ def test_iloc_mask(self): | |
(None, ".iloc"): "0b1100", | ||
("index", ""): "0b11", | ||
("index", ".loc"): "0b11", | ||
("index", ".iloc"): ( | ||
"iLocation based boolean indexing cannot use an indexable as a mask" | ||
), | ||
("locs", ""): "Unalignable boolean Series provided as indexer " | ||
"(index of the boolean Series and of the indexed " | ||
"object do not match).", | ||
("locs", ".loc"): "Unalignable boolean Series provided as indexer " | ||
"(index of the boolean Series and of the " | ||
"indexed object do not match).", | ||
("locs", ".iloc"): ( | ||
"iLocation based boolean indexing on an integer type is not available" | ||
), | ||
( | ||
"index", | ||
".iloc", | ||
): "iLocation based boolean indexing cannot use an indexable as a mask", | ||
("locs", ""): "Unalignable boolean Series provided as indexer", | ||
("locs", ".loc"): "Unalignable boolean Series provided as indexer", | ||
("locs", ".iloc"): "Unalignable boolean Series provided as indexer", | ||
} | ||
|
||
# UserWarnings from reindex of a boolean mask | ||
for idx in [None, "index", "locs"]: | ||
mask = (df.nums > 2).values | ||
if idx: | ||
mask_index = getattr(df, idx)[::-1] | ||
mask_index = getattr(df, idx if idx == "index" else "locs")[::-1] | ||
mask = Series(mask, list(mask_index)) | ||
for method in ["", ".loc", ".iloc"]: | ||
try: | ||
|
@@ -780,18 +776,29 @@ def test_iloc_mask(self): | |
else: | ||
accessor = df | ||
answer = str(bin(accessor[mask]["nums"].sum())) | ||
except (ValueError, IndexingError, NotImplementedError) as err: | ||
except ( | ||
ValueError, | ||
IndexingError, | ||
) as err: | ||
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. nit: Can you collapse this back to one line |
||
answer = str(err) | ||
|
||
key = ( | ||
idx, | ||
method, | ||
) | ||
r = expected.get(key) | ||
if r != answer: | ||
raise AssertionError( | ||
f"[{key}] does not match [{answer}], received [{r}]" | ||
expected_result = expected.get(key) | ||
|
||
# Fix the assertion to check for substring match | ||
if ( | ||
idx is None or (idx == "index" and method != ".iloc") | ||
) and "0b" in expected_result: | ||
# For successful numeric results, exact match is needed | ||
assert expected_result == answer, ( | ||
f"[{key}] does not match [{answer}]" | ||
) | ||
else: | ||
# For error messages, substring match is sufficient | ||
assert expected_result in answer, f"[{key}] not found in [{answer}]" | ||
|
||
def test_iloc_non_unique_indexing(self): | ||
# GH 4017, non-unique indexing (on the axis) | ||
|
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.
Since
idx
is notNone
here, I think this can be reverted.