Skip to content

fix: apply coords-as-truth rule to mask in add_variables/add_constraints#725

Open
FBumann wants to merge 1 commit into
fix/bounds-coords-broadcastfrom
fix/mask-coords-broadcast
Open

fix: apply coords-as-truth rule to mask in add_variables/add_constraints#725
FBumann wants to merge 1 commit into
fix/bounds-coords-broadcastfrom
fix/mask-coords-broadcast

Conversation

@FBumann
Copy link
Copy Markdown
Collaborator

@FBumann FBumann commented May 24, 2026

Stacked on #722. Extends the same coords-as-truth rule to mask.

  • mask in add_variables / add_constraints now routes through as_dataarray_in_coords(mask, data.coords). Pandas Series / DataFrame masks missing a dim are broadcast like bounds; mask type hint on add_variables widens to MaskLike.
  • broadcast_mask had no other callers and is deleted.

Breaking change (delivering on the previously-emitted FutureWarning):

  • Sparse-coord masks raise ValueError instead of silently filling missing entries with False. Workaround: mask.reindex({...}, fill_value=False).
  • Extra-dim masks raise ValueError instead of AssertionError.

@FBumann
Copy link
Copy Markdown
Collaborator Author

FBumann commented May 24, 2026

@FabianHofmann THis is somewhat breaking, but we had a Future Warning in place. It aligns the treatment of vounds and mask in add_variables, which was uninuitively differing before.
It also fits very well with #722.

@FBumann FBumann requested a review from FabianHofmann May 24, 2026 12:05
FBumann added a commit that referenced this pull request May 24, 2026
…ds keys

Numpy and unnamed-pandas inputs are positional — there are no names
on the input to map to dims, so passing them through ``as_dataarray``
without a ``dims`` kwarg makes them fall back to ``dim_0/dim_1/...``.
The subsequent ``set(arr.dims) - set(expected)`` check then sees
those as "extra dimensions" and raises ``ValueError``, even when the
input has the right shape for the coords.

This regressed the ``add_variables(..., mask=numpy_mask, coords=[a,
b])`` case from the ``creating-variables`` notebook (and broke the
RTD docs build for #725 because of it).

Default ``dims`` to the coords' dim names when not supplied, so a
2-D numpy mask paired with 2-D coords gets the right labels and
flows through validate/expand/transpose normally.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@FBumann FBumann force-pushed the fix/mask-coords-broadcast branch 2 times, most recently from ffb83b9 to 681728d Compare May 24, 2026 13:25
…onstraints

Routes ``mask`` through ``as_dataarray_in_coords(mask, data.coords)``
instead of ``as_dataarray(...) + broadcast_mask(...)``, so pandas
``Series`` / ``DataFrame`` masks missing a dimension are broadcast
to the variable / constraint shape (parallel to the bounds fix in
the previous PR). The ``add_variables`` ``mask`` type hint widens
to ``MaskLike`` to match ``add_constraints``.

The deprecation announced via ``FutureWarning`` in ``broadcast_mask``
("Missing values will be filled with False ... In a future version,
this will raise an error") is now in effect: masks whose
coordinates are a sparse subset of the data's coordinates raise
``ValueError`` instead of silently filling missing entries.
Mask dims not in the data raise ``ValueError`` instead of
``AssertionError`` for consistency with the bounds path.

``broadcast_mask`` had no other callers and is removed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@FBumann FBumann force-pushed the fix/mask-coords-broadcast branch from 681728d to 75f33bc Compare May 24, 2026 13:37
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