Skip to content

fix(common): proper MultiIndex support in coords helpers#729

Merged
FabianHofmann merged 1 commit into
fix/bounds-coords-broadcastfrom
fix/bounds-multiindex-support
May 27, 2026
Merged

fix(common): proper MultiIndex support in coords helpers#729
FabianHofmann merged 1 commit into
fix/bounds-coords-broadcastfrom
fix/bounds-multiindex-support

Conversation

@FabianHofmann
Copy link
Copy Markdown
Collaborator

Summary

Builds on #722 to close gaps in MultiIndex coord handling:

  • _coords_to_dict: Explicitly handles pd.MultiIndex — registers under .name if set, raises TypeError with guidance if .name is missing (xarray requires a dimension name)
  • _named_pandas_to_dataarray: Uses DataArray(df) directly for single-level-index DataFrames (avoids unnecessary stack() + to_xarray() round-trip); reserves stack() for MultiIndex axes where it's genuinely needed
  • as_dataarray_in_coords: Validates MultiIndex dimensions with .equals() instead of silently skipping them — catches mismatched MultiIndex bounds that previously slipped through
  • Tests: Moves MultiIndex tests into dedicated TestAddVariablesMultiIndexCoords class with shared midx fixture; adds broadcast, unnamed-raises, and mismatch-raises coverage

Test plan

  • pytest test/test_variable.py::TestAddVariablesMultiIndexCoords — 5/5
  • pytest test/test_variable.py::TestAddVariablesBoundsWithCoords — 56/56
  • pytest test/test_variable.py test/test_model.py test/test_io.py test/test_piecewise_constraints.py — 413 pass, no regressions
  • pytest test/test_linear_expression.py test/test_constraint.py test/test_constraints.py — 393 pass
  • ruff check + format clean

- _coords_to_dict: explicitly handle pd.MultiIndex — register under
  .name if set, raise TypeError with guidance if .name is missing
- _named_pandas_to_dataarray: use DataArray(df) directly for
  single-level DataFrames; reserve stack() for MultiIndex axes
- as_dataarray_in_coords: validate MultiIndex dims with .equals()
  instead of silently skipping them
- Move MultiIndex tests into dedicated TestAddVariablesMultiIndexCoords
  class with shared fixture
@FabianHofmann FabianHofmann requested a review from FBumann May 27, 2026 08:38
@FabianHofmann FabianHofmann merged commit 68c4e09 into fix/bounds-coords-broadcast May 27, 2026
3 checks passed
@FabianHofmann FabianHofmann deleted the fix/bounds-multiindex-support branch May 27, 2026 08:54
FBumann added a commit that referenced this pull request May 27, 2026
#729 made `.name` required on `pd.MultiIndex` sequence-form coord entries
(xarray needs a single dim name for the flattened index). test_repr.py was
the only remaining call site missing the assignment.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

2 participants