Skip to content

feat(groupby): fast path for multi-key groupby([names]), flat + non-breaking#755

Merged
FBumann merged 1 commit into
fix/groupby-coord-namefrom
feat/groupby-multikey-fast-flat
Jun 4, 2026
Merged

feat(groupby): fast path for multi-key groupby([names]), flat + non-breaking#755
FBumann merged 1 commit into
fix/groupby-coord-namefrom
feat/groupby-multikey-fast-flat

Conversation

@FBumann
Copy link
Copy Markdown
Collaborator

@FBumann FBumann commented Jun 4, 2026

What

Makes multi-key grouping by coordinate names — expr.groupby(["a", "b"]).sum() — take the fast reindex path instead of the slow xarray fallback, while keeping its flat output and changing nothing about the existing DataFrame grouper. Implements the validated, non-breaking direction from #753.

Stacked on top of #751 (fix/groupby-coord-name) — it builds on the _resolve_group helper introduced there. Merge #751 first.

How

  • A list/tuple of coordinate names (1-D, sharing one dimension) is resolved to a value frame so it rides the existing reindex fast path, then the stacked result is unstacked back into one dim per name. The output is byte-identical to the old fallback, sparse fill cells included (verified with assert_linequal).
  • groupby(df) (user-supplied DataFrame) is untouched — it keeps its compact stacked-MultiIndex output over observed combinations only. So this is non-breaking; the tested DataFrame contract still holds.

Memory note

Flat dims are a dense cartesian grid, so a sparse key crossing materialises mostly-fill cells (measured ~100× vs the DataFrame grouper for a diagonal crossing — see #740). A UserWarning fires when the grid is much larger than the observed combinations, pointing users at the compact DataFrame grouper. The check reads the collapsed MultiIndex levels, so it is O(observed), not O(N), and fires before unstack allocates the grid.

This is fundamental to dense xarray (separate dims = dense grid); truly flat-and-compact needs a sparse/long-format kernel — tracked as "Future directions" on #753 and as evidence on #740.

Tests

Six new cases in TestGroupbyByAttachedCoordinate: fast-path == fallback (list/tuple, sparse), flat-not-stacked, sparse-combination-filled, DataFrame-grouper-stays-compact, warns-when-sparse, silent-when-dense. Full groupby suite (58) + ruff + mypy green.

Closes #753.

🤖 Generated with Claude Code

…reaking

`groupby(["a","b"]).sum()` previously dropped to the slow xarray fallback.
Resolve a list of coordinate names (1-D, same dim) to a value frame so it
rides the existing reindex fast path, then unstack the stacked result back
into flat per-name dims -- byte-identical to the fallback, sparse fill cells
included. The DataFrame grouper is untouched and stays compact (stacked
MultiIndex over observed combinations only), so this is non-breaking.

Flat dims are a dense cartesian grid, so a sparse key crossing materialises
mostly-fill cells. Warn (pointing at the DataFrame grouper) when the grid is
much larger than the observed combinations; the check reads the collapsed
MultiIndex levels, so it is O(observed) and fires before unstack allocates.

See #753; sparse-representation follow-ups tracked against #740.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@FBumann FBumann merged commit 63e82ef into fix/groupby-coord-name Jun 4, 2026
1 of 2 checks passed
@FBumann FBumann deleted the feat/groupby-multikey-fast-flat branch June 4, 2026 14:37
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.

1 participant