Fix add_variables ignoring coords for DataArray bounds#614
Open
Fix add_variables ignoring coords for DataArray bounds#614
Conversation
When DataArray bounds were passed to add_variables with explicit coords, the coords parameter was silently ignored because as_dataarray skips conversion for DataArray inputs. Now validates DataArray bounds against coords: raises ValueError on mismatched or extra dimensions, and broadcasts missing dimensions via expand_dims. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Test MultiIndex coords (validation skip), xarray Coordinates object, dims-only DataArrays, and upper bound mismatch detection. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Infer dims from dict keys when dims is None and the input is a scalar. Previously this raised xarray's CoordinateValidationError because xarray can't broadcast a 0-dim value to coords without explicit dims. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Consolidate add_variables tests into TestAddVariablesBoundsWithCoords class with parameterized tests covering all bound types (scalar, np.number, numpy, pandas, list, DataArray, DataArray-no-coords) x both coord formats (sequence, dict). Also fixes as_dataarray for scalars with dict coords by inferring dims from dict keys. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Test DataArray+numpy, DataArray+scalar, DataArray+DataArray combos for lower/upper. Also test both bounds covering different dim subsets with broadcast, and that only the mismatched bound raises ValueError. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add numpy-0d and dataarray-0d to the parameterized bound type tests. Fix numpy_to_dataarray to infer dims from dict keys for 0-dim arrays, matching the scalar fix in as_dataarray. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Cover three gaps: coords inferred from bounds (no coords arg) for DataArray and pandas, multi-dimensional coord specifications with both scalar and DataArray bounds, and real-world coordinate types (string regions, datetime index) including mismatch detection. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Verify lower(time, space) and upper(space, time) align correctly via xarray broadcast. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When a DataArray bound has the same coordinate values as coords but in a different order, reindex to match instead of raising ValueError. Still raises when the values actually differ (not just reordered). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This was referenced Mar 13, 2026
Remove unreachable hasattr(coords, "dims") branch in _coords_to_dict (xarray Coordinates are Mappings, caught by isinstance check above). Add Any type annotations to parameterized test arguments. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
|
@FabianHofmann Sorry for all the branches. I really wanted to make the fix more universal and establish a distinction between converting to a dataarray and converting to a dataarray and validate, but i hot lost in the process. |
FBumann
added a commit
to FBumann/fluxopt
that referenced
this pull request
Mar 13, 2026
Pin to the fix/add-variables-dataarray-coords branch which fixes DataArray coords handling in add_variables. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
Two bugs in
add_variableswhen bounds are DataArrays andcoordsis provided. Bothlowerandupperare affected identically.This is a focused extract from #551 — bug fixes only, no
as_dataarrayrefactoring.Bug 1: DataArray coords silently ignored → NaN bounds
The fix validates DataArray bounds against
coords: mismatched or extra dims raiseValueError, missing dims are broadcast viaexpand_dims, reordered coords (same values) are reindexed.Bug 2: Dict coords crash for all input types
Fixed by inferring
dimsfrom dict keys inas_dataarrayandnumpy_to_dataarrayfor scalar/0-dim inputs.Changes
linopy/model.py_validate_dataarray_bounds+_coords_to_dicthelpers, called inadd_variableslinopy/common.pydimsfrom dict keys for scalars/0-dim arraystest/test_common.pyNo existing tests were modified or removed — all changes to
test/test_common.pyare purely additive. All 32 existing test functions from master are preserved.Test results: master vs this branch
CoordinateValidationError)ValueErrorValueErrorFull test suite: 2570+ passed, 236 skipped.
🤖 Generated with Claude Code