Skip to content
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 boolean masks for iloc.__getitem__ #61162

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

arthurlw
Copy link
Contributor

@arthurlw arthurlw commented Mar 21, 2025

@arthurlw arthurlw changed the title Pass boolean to iloc. getitem Support boolean masks for iloc.__getitem__ Mar 21, 2025
@arthurlw arthurlw changed the title Support boolean masks for iloc.__getitem__ BUG: Support boolean masks for iloc.__getitem__ Mar 21, 2025
@arthurlw arthurlw changed the title BUG: Support boolean masks for iloc.__getitem__ ENH: Support boolean masks for iloc.__getitem__ Mar 21, 2025
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Am I correct that none of the tests being modified here actually call .iloc successfully?

}

# 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]
Copy link
Member

Choose a reason for hiding this comment

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

Since idx is not None here, I think this can be reverted.

Comment on lines 779 to 782
except (
ValueError,
IndexingError,
) as err:
Copy link
Member

Choose a reason for hiding this comment

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

nit: Can you collapse this back to one line

@rhshadrach rhshadrach added Enhancement Indexing Related to indexing on series/frames, not to indexes themselves labels Mar 29, 2025
@arthurlw
Copy link
Contributor Author

arthurlw commented Mar 29, 2025

Hi! The test on lines 742-743 calls .iloc with a boolean mask successfully:

result = df.iloc[np.array([True] * len(mask), dtype=bool)]
tm.assert_frame_equal(result, df)

I noticed I should add another test with false values so I have done so.

result2 = df.iloc[np.array([True, False, True, False, True], dtype=bool)]
tm.assert_frame_equal(result2, DataFrame({"a": [0, 2, 4]}, index=["A", "C", "E"]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: iloc with Series as indexer fails for __getitem__ but works with __setitem__
2 participants