Fix as_dataarray to validate coords for DataArray inputs#613
Closed
Fix as_dataarray to validate coords for DataArray inputs#613
Conversation
Previously, as_dataarray silently ignored the coords parameter when the input was already a DataArray — mismatched coordinates would produce NaN-filled garbage via xarray's outer join. Now, when coords is provided, DataArray inputs are strictly validated: shared dims must match exactly, extra dims are rejected, and missing dims are broadcast via expand_dims. Arithmetic call sites (expressions, variables) skip as_dataarray for DataArray inputs since they handle alignment themselves. Mask call sites in add_variables/add_constraints also skip validation to preserve broadcast_mask's existing fill-with-False behavior for partial coverage. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…y (validation) ensure_dataarray: converts any supported type to DataArray without coordinate validation. DataArray inputs pass through unchanged. Used by arithmetic call sites that handle alignment themselves. as_dataarray: calls ensure_dataarray then validates DataArray coords when coords is provided — shared dims must match exactly, extra dims are rejected, missing dims are broadcast via expand_dims. This fixes the bug where DataArray bounds with mismatched coords were silently accepted, producing NaN-filled garbage via xarray's outer join. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Collaborator
Author
|
Closed in favor of #614 |
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.
Motivation
add_variablessilently produced wrong results when DataArray bounds had mismatched coordinates with thecoordsparameter. The root cause wasas_dataarray— it appliedcoordsfor all input types (numpy, pandas, scalars) but silently ignored it for DataArray inputs, letting them pass through unchanged. This caused xarray's outer join to produce NaN-filled bounds with wrong shapes.What changed
as_dataarraynow validates DataArray inputs whencoordsis provided:ValueErroron mismatchValueErrorexpand_dimsensure_dataarrayis a new internal method for pure type conversion without validation or expansion. Arithmetic call sites (expressions.py,variables.py) use this instead, since they handle alignment themselves via_align_constant/reindex_like.Supersedes #551.
Test plan
add_variablesintegration🤖 Generated with Claude Code