Repository navigation
Fix Dataset.weighted reductions over a dimension missing from a variable and the weights - #11903
Open
DresdenGman wants to merge 2 commits into
Open
DresdenGman wants to merge 2 commits into
DresdenGman wants to merge 2 commits into
Conversation
…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
force-pushed
the
fix-8679-weighted-missing-dim
branch
from
November 26, 2026 03:43
4e37f3a to
7fd2f48
Compare
Author
|
Rebased onto the latest Re-ran the tests on the rebased branch: |
This branch has not been deployed
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.
Description
Weighted reductions on a
Datasetraise 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:This is a regression from #8606, which made
dotraise if a reduction dimension is not present on either of its two inputs. That check is right for direct calls todot, butWeighted._reduceappliesdotto 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, anddotused to simply ignore such dimensions.The fix restores the pre-#8606 behaviour in
Weighted._reduceonly: before callingdot, the requested dimensions are filtered down to those present on the variable or on the weights. Nothing else changes:dotkeeps its existence check for direct callers.Weighted._check_dim, with the same error as before (covered by existing and new tests)._check_dimalready guarantees the dimension is on the array or the weights.This restores
sum,meanandsum_of_weightsfor this case (the operations that go throughWeighted._reduce→dot).var/std/sum_of_squares/quantilefailed on this input both before and after #8606 for unrelated reasons and are not changed here.Tests: new cases in
xarray/tests/test_weighted.pycover the reported example formean/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. Runningxarray/tests/test_weighted.pyandxarray/tests/test_computation.pytogether: 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 checkandruff format --checkpass on the changed files.Checklist
whats-new.rstAI Disclosure
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.