Fix Dataset.isin and DataArray.isin when passed a set - #11525
Open
NoiceHax wants to merge 1 commit into
Open
Conversation
numpy turns a set into a 0d object array, so Dataset.isin and DataArray.isin returned False everywhere when given a set. Convert set-like arguments to a list before handing them to numpy. Co-authored-by: Claude <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
isinpassed its argument straight to numpy, and numpy turns a set into a 0-dimensional object array. That array never compares equal to any element, soxr.DataArray([1, 2, 3, 4]).isin({1, 2})came back False everywhere instead of matching 1 and 2. The same happened with afrozensetor withdict.keys(). pandas returns the answer you would expect here, so the difference is easy to trip over.The fix adds one branch to the existing isinstance chain in
DataWithCoords.isinthat converts acollections.abc.Setto a list before the numpy call. That covers sets, frozensets and dict views. Passing aDatasetstill raisesTypeErroras before.Tests: set and frozenset added to the parametrize on
test_isinintest_dataset.py, set anddict.keys()added totest_isinintest_dataarray.py. Both fail before the change.The issue thread also floats raising for scalars and strings the way pandas does. That is a larger behavior change and is not part of this PR.
Checklist
whats-new.rstapi.rstAI Disclosure
Tools: Claude Code. I asked it to special-case sets in
isinas suggested in the issue, then reviewed the change and ran the isin tests and pre-commit locally.