Skip to content

Fix as_dataarray to validate coords for DataArray inputs#613

Closed
FBumann wants to merge 3 commits intomasterfrom
fix/as-dataarray-v2
Closed

Fix as_dataarray to validate coords for DataArray inputs#613
FBumann wants to merge 3 commits intomasterfrom
fix/as-dataarray-v2

Conversation

@FBumann
Copy link
Collaborator

@FBumann FBumann commented Mar 12, 2026

Motivation

add_variables silently produced wrong results when DataArray bounds had mismatched coordinates with the coords parameter. The root cause was as_dataarray — it applied coords for 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_dataarray now validates DataArray inputs when coords is provided:

  • Shared dims must match exactly → ValueError on mismatch
  • Extra dims in the DataArray → ValueError
  • Missing dims (in coords but not DataArray) → broadcast via expand_dims

ensure_dataarray is 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

  • 8 new tests covering matching, mismatch, extra dims, broadcast, sequence coords, and add_variables integration
  • Full test suite passes (2553 passed)
  • Linting and formatting pass

🤖 Generated with Claude Code

FBumann and others added 2 commits March 12, 2026 18:54
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>
@FBumann FBumann marked this pull request as draft March 12, 2026 18:11
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@FBumann
Copy link
Collaborator Author

FBumann commented Mar 13, 2026

Closed in favor of #614

@FBumann FBumann closed this Mar 13, 2026
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