Conversation
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>
|
@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? |
|
I like the shuffling, but i feel like this is reimplementing a bunch of |
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>
You were right, now the logic is combined in |
| ): | ||
| return False | ||
|
|
||
| # Flox can rechunk contiguous groups on its own, e.g. for resampling, so |
There was a problem hiding this comment.
for resampling we store slice object in group_indices IIRC so that's a cheap short cut that we can exploit here
dcherian
left a comment
There was a problem hiding this comment.
Would be good to port upstream. We'll need to maintain this here for a while anyway though
Description
Since #11239,
groupby(...).median()on dask arrays uses flox withmethod="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']withchunks=1), each block returns the same group again and flox fails withInvalidIndexError: Reindexing only valid with uniquely valued Index objects.This PR shuffles the object (and the grouper codes) with
dask.array.shufflebefore calling flox, whenevermethod="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 passmethod="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
whats-new.rstAI Disclosure
Tools: Claude Code (Claude Opus 5.5)
[This is Claude Code on behalf of Michael Niklas]
🤖 Generated with Claude Code