fix(common): proper MultiIndex support in coords helpers#729
Merged
FabianHofmann merged 1 commit intoMay 27, 2026
Merged
Conversation
- _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
FBumann
approved these changes
May 27, 2026
This was referenced May 27, 2026
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>
4 tasks
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.
Summary
Builds on #722 to close gaps in MultiIndex coord handling:
_coords_to_dict: Explicitly handlespd.MultiIndex— registers under.nameif set, raisesTypeErrorwith guidance if.nameis missing (xarray requires a dimension name)_named_pandas_to_dataarray: UsesDataArray(df)directly for single-level-index DataFrames (avoids unnecessarystack()+to_xarray()round-trip); reservesstack()for MultiIndex axes where it's genuinely neededas_dataarray_in_coords: Validates MultiIndex dimensions with.equals()instead of silently skipping them — catches mismatched MultiIndex bounds that previously slipped throughTestAddVariablesMultiIndexCoordsclass with sharedmidxfixture; adds broadcast, unnamed-raises, and mismatch-raises coverageTest plan
pytest test/test_variable.py::TestAddVariablesMultiIndexCoords— 5/5pytest test/test_variable.py::TestAddVariablesBoundsWithCoords— 56/56pytest test/test_variable.py test/test_model.py test/test_io.py test/test_piecewise_constraints.py— 413 pass, no regressionspytest test/test_linear_expression.py test/test_constraint.py test/test_constraints.py— 393 pass