Registry Mirror
Skip to content

Conversation

@headtr1ck

@headtr1ck headtr1ck commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Since #11239, groupby(...).median() on dask arrays uses flox with method="blockwise". flox can only rechunk so that chunk boundaries line up with group boundaries if the members of each group are contiguous along the grouped dimension. With interleaved labels spread over multiple chunks (e.g. ['1', '2', '1', '2'] with chunks=1), each block returns the same group again and flox fails with InvalidIndexError: Reindexing only valid with uniquely valued Index objects.

This PR shuffles the object (and the grouper codes) with dask.array.shuffle before calling flox, whenever method="blockwise" is used, the grouped dimension has multiple chunks, and the groups are not contiguous. This also fixes the same failure for other reductions when users pass method="blockwise" explicitly. Contiguous groups (e.g. resample) are unchanged and still handled by flox's rechunking. Grouping by multi-dimensional or chunked arrays is not affected by this change.

Once xarray-contrib/flox#501 is fixed, flox would shuffle on its own; the shuffle here stays correct (flox finds nothing left to do) and can then be restricted to older flox versions. There's a TODO in the code for that.

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 (Claude Opus 5.5)

[This is Claude Code on behalf of Michael Niklas]

🤖 Generated with Claude Code

Since pydata#11239, groupby median on dask arrays uses flox with
method="blockwise". flox can only rechunk so that chunk boundaries line
up with group boundaries if the members of each group are contiguous.
With interleaved labels spread over multiple chunks this raised an
InvalidIndexError. Shuffle every group into a single chunk first.

This can be restricted to older flox versions once
xarray-contrib/flox#501 is fixed.

Fixes pydata#11651

Co-authored-by: Claude <noreply@anthropic.com>
@headtr1ck

Copy link
Copy Markdown
Collaborator Author

@dcherian do you prefer to just fallback to without flox in this case instead until the upstream supports this? Or is it fine to have this workaround until then?

@dcherian

dcherian commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

I like the shuffling, but i feel like this is reimplementing a bunch of shuffle_to_chunks? why can'y we use _shuffle_obj? And if not, can we improve _shuffle_obj

headtr1ck and others added 3 commits October 1, 2026 21:53
Instead of a second implementation, _shuffle_obj now also shuffles the
codes of the groupers, so that they match the shuffled object, and
returns both. shuffle_to_chunks only uses the shuffled object. It also
doesn't add the grouper variables to the original object in place
anymore.

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
@headtr1ck

Copy link
Copy Markdown
Collaborator Author

I like the shuffling, but i feel like this is reimplementing a bunch of shuffle_to_chunks? why can'y we use _shuffle_obj? And if not, can we improve _shuffle_obj

You were right, now the logic is combined in _shuffle_obj.

Comment thread xarray/core/groupby.py
):
return False

# Flox can rechunk contiguous groups on its own, e.g. for resampling, so

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

for resampling we store slice object in group_indices IIRC so that's a cheap short cut that we can exploit here

@dcherian dcherian left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would be good to port upstream. We'll need to maintain this here for a while anyway though

@dcherian
dcherian merged commit d0366c6 into pydata:main Oct 2, 2026
45 checks passed
@headtr1ck
headtr1ck deleted the fix-groupby-median-dask branch October 9, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Group by median fails with dask arrays with xarray==2026.9.0

2 participants