Registry Mirror
Skip to content

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.
@NoiceHax

NoiceHax commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main (d4bce83a).

One conflict, in xarray/tests/test_dataset.py: main has since changed test_isin's signature from backend to use_dask: bool, while this branch had widened its parametrize list with {1, 2} and frozenset({1, 2}). Resolved by taking both — the current signature with the set cases added:

@pytest.mark.parametrize(
    "test_elements",
    ([1, 2], np.array([1, 2]), DataArray([1, 2]), {1, 2}, frozenset({1, 2})),
)
def test_isin(test_elements, use_dask: bool) -> None:

pytest xarray/tests/test_dataset.py -k isin → 6 passed, 5 skipped (the skips are the dask variants; dask is not installed locally).

@headtr1ck headtr1ck left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks reasonable to me.
Technically that's a breaking change but I assume nobody would actually use numpy arrays of sets, haha.

Comment thread xarray/core/common.py Resolved
@headtr1ck headtr1ck added the plan to merge Final call for comments label Oct 12, 2026
Co-authored-by: Claude <noreply@anthropic.com>
@headtr1ck
headtr1ck enabled auto-merge (squash) October 12, 2026 20:46
@headtr1ck
headtr1ck merged commit bdefbed into pydata:main Oct 12, 2026
44 of 46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataArray.isin does not accept sets intuitively

2 participants