Skip to content

Fix Dataset.weighted reductions over a dimension missing from a variable and the weights - #11903

Open
DresdenGman wants to merge 2 commits into
pydata:mainfrom
DresdenGman:fix-8679-weighted-missing-dim
Open

DresdenGman wants to merge 2 commits into
pydata:mainfrom
DresdenGman:fix-8679-weighted-missing-dim

Conversation

@DresdenGman

Copy link
Copy Markdown

Description

Weighted reductions on a Dataset raise an error when reducing over a dimension that exists on some variables of the Dataset but on neither one of the other variables nor the weights:

import xarray as xr

ds = xr.Dataset({"a": (("y", "x"), [[1, 2]]), "scalar": 1})
weights = xr.DataArray([1, 2], dims="x")
ds.weighted(weights).mean("y")
# ValueError: Dimension(s) 'y' do not exist. Expected one or more of {'x'}

This is a regression from #8606, which made dot raise if a reduction dimension is not present on either of its two inputs. That check is right for direct calls to dot, but Weighted._reduce applies dot to each variable of a Dataset separately, with a dimension that was validated against the whole Dataset and the weights — an individual variable (like the scalar above) may have neither that dimension nor share it with the weights, and dot used to simply ignore such dimensions.

The fix restores the pre-#8606 behaviour in Weighted._reduce only: before calling dot, the requested dimensions are filtered down to those present on the variable or on the weights. Nothing else changes:

  • dot keeps its existence check for direct callers.
  • Dimensions missing from the whole Dataset and the weights are still rejected earlier by Weighted._check_dim, with the same error as before (covered by existing and new tests).
  • DataArray reductions are unaffected: for a single array, _check_dim already guarantees the dimension is on the array or the weights.

This restores sum, mean and sum_of_weights for this case (the operations that go through Weighted._reduce → dot). var/std/sum_of_squares/quantile failed on this input both before and after #8606 for unrelated reasons and are not changed here.

Tests: new cases in xarray/tests/test_weighted.py cover the reported example for mean/sum/sum_of_weights, variables with disjoint dimensions, dimension lists containing a fully missing dimension (still an error), a dimension that exists only on the weights, order-independence of dimension lists, and ... reductions. The new regression tests fail without the fix. Running xarray/tests/test_weighted.py and xarray/tests/test_computation.py together: 819 passed, 82 skipped, 2 xfailed, 2 xpassed (baseline before the change: 808 passed, same skips/xfails — the difference is exactly the new tests). ruff check and ruff format --check pass on the changed files.

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude

The approach and design of this fix are my own. AI assistance was used only to help implement the code and tests; I reviewed the changes, understood them, and tested them as described above.

…ble and the weights

Weighted reductions apply dot() to each variable of a Dataset
separately. Since GH8606, dot() raises if the reduction dimension is
not present on either the variable or the weights, which broke
reducing a Dataset over a dimension that only exists on other
variables (GH8679). Drop such dimensions in Weighted._reduce, as
dot() used to silently ignore them.
Pin the behaviour the per-variable dimension filter in
Weighted._reduce relies on: dimensions missing from the whole
object and the weights are still rejected by _check_dim, also in
mixed dimension lists; a dimension that only exists on the
weights is kept; the order of requested dimensions does not
affect the result; and ... reduces over all dimensions.
@DresdenGman
DresdenGman force-pushed the fix-8679-weighted-missing-dim branch from 4e37f3a to 7fd2f48 Compare November 26, 2026 03:43
@DresdenGman

Copy link
Copy Markdown
Author

Rebased onto the latest main and resolved the merge conflicts (several weighted-related changes had landed upstream since this branch was last updated, including #11833, #11868 and #11921). The change itself is unchanged: same 3 files, +125/-0.

Re-ran the tests on the rebased branch: xarray/tests/test_weighted.py and xarray/tests/test_computation.py together give 851 passed, 82 skipped, 2 xfailed, 2 xpassed (the higher pass count vs. the original 819 is from new upstream tests; skips/xfails unchanged). ruff check and ruff format --check pass on the changed files.

This branch has not been deployed

No deployments
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.

Dataset.weighted along a dimension not on weights errors

1 participant