Skip to content

Fix Dataset.isin and DataArray.isin when passed a set - #11525

Open
NoiceHax wants to merge 1 commit into
pydata:mainfrom
NoiceHax:fix/issue-10022
Open

Fix Dataset.isin and DataArray.isin when passed a set#11525
NoiceHax wants to merge 1 commit into
pydata:mainfrom
NoiceHax:fix/issue-10022

Conversation

@NoiceHax

Copy link
Copy Markdown
Contributor

Description

isin passed its argument straight to numpy, and numpy turns a set into a 0-dimensional object array. That array never compares equal to any element, so xr.DataArray([1, 2, 3, 4]).isin({1, 2}) came back False everywhere instead of matching 1 and 2. The same happened with a frozenset or with dict.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.isin that converts a collections.abc.Set to a list before the numpy call. That covers sets, frozensets and dict views. Passing a Dataset still raises TypeError as before.

Tests: set and frozenset added to the parametrize on test_isin in test_dataset.py, set and dict.keys() added to test_isin in test_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

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code. I asked it to special-case sets in isin as suggested in the issue, then reviewed the change and ran the isin tests and pre-commit locally.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataArray.isin does not accept sets intuitively

1 participant