Repository navigation
Conversation
|
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
Shuffle the object so each group is contained in one chunk, apply the function once per block with map_blocks, then restore the original order. For out-of-order group labels this reduces the dask graph from O(groups) tasks to O(chunks), with a template fast path and full-inference fallback. Closes pydata#9220. Builds on the approach explored in pydata#9706. Co-Authored-By: Claude Code <noreply@anthropic.com>
PStarH
force-pushed
the
groupby-map-shuffle
branch
from
November 13, 2026 03:19
b716fb9 to
3c0e6e5
Compare
Counting group occurrences with a groupby reduction may dispatch to flox/numbagg, which raises a numba TypingError for object-like group labels such as intervals. Use pandas value_counts instead; the result is reindexed to the full group index exactly as before. Co-Authored-By: Claude Code <noreply@anthropic.com>
This branch has not been deployed
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.
Problem
GroupBy.mapbuilds one task and one graph layer per group. For dask-backed data with many groups this dominates both graph construction and scheduling; #9220 shows the effect for out-of-order labels such astime.dayofyear:100×100×4017, chunks
(100, 100, 365),map(lambda g: g - g.mean("time")):map(...)map(..., shuffle=True)Approach
Add a keyword-only
shuffle: bool = FalsetoDataArrayGroupBy.mapandDatasetGroupBy.map:When the object is chunked and
shuffle=True:_shuffle_obj, so every group is contained in one chunk;funconce per block withmap_blocks, using an innergroupby(...).map;_inverse_permutation_indices+_shuffle.A cheap template is inferred by applying
functo one group and reusing the shuffled object's metadata (dtypes only); functions that change dims/variables fall back to full per-group inference. Reductions are reindexed to the full group index, so empty groups (bins/resample-style) match the baseline, including NaNs.The default (
shuffle=False) and non-chunked inputs use the existing code path unchanged. New tests compare against the baseline withassert_identical.This builds on the approach explored in the draft #9706, updated to current
mainwith tests, docs and the empty-group reduction fix.Benchmarks
Compute on 55×55×2191 (≈53 MB),
scheduler="threads", graph build + compute per run, median of 3: 1.317 s → 0.296 s (4.45×; baseline runs 1.372 / 1.317 / 1.280 s, shuffle runs 0.298 / 0.272 / 0.296 s).Limitations
funcmust preserve the size of the grouped dimension; shrinking/growing functions raiseNotImplementedErrorwhenshuffle=True.shuffle=True(chunked groupers are also unsupported bymaptoday).argsand functions that change coordinate values are not covered by the template fast path yet.shortcutis accepted but has no effect in the shuffled path.Tests
15 new tests cover element-preserving DataArray/Dataset, mixed dtypes, derived and out-of-order groupers, reductions (including empty groups), NaN groups, bins, args/kwargs, non-chunked fallback, graph size, and
raise_if_dask_computes.Regression sweep (
test_groupby + test_dask + test_rolling + test_cftimeindex_resample): 4,006 passed, 773 skipped, 3 xfailed. Full suite: 18,365 passed, 3,634 skipped, 65 xfailed, 24 xpassed (337 s, exit 0).This PR was prepared with AI assistance (Claude); all reported numbers were measured on the patched tree, and every supported case is checked against the baseline with
assert_identical.🤖 Generated with Claude Code