pydata / pydata/xarray

Weighted reductions drop orphaned dimensions

Open
#11,560 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs triage
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

Weighted reductions drop orphaned dimensions - unlike their non-weighted versions. See the MRE, but TLDR;

********************  base array  ********************
<xarray.Dataset> Size: 56B
Dimensions:    (lat: 3, time: 1)
Coordinates:
  * lat        (lat) int64 24B 0 1 2
  * time       (time) int64 8B 0
Data variables:
    elevation  (lat) float64 24B 0.0 1.0 2.0
********************  weighted mean: base.weighted(weights).mean('lat') ********************
<xarray.Dataset> Size: 8B
Dimensions:    ()
Data variables:
    elevation  float64 8B 1.0
********************  regular mean: base.mean('lat') ********************
<xarray.Dataset> Size: 16B
Dimensions:    (time: 1)
Coordinates:
  * time       (time) int64 8B 0
Data variables:

I'll open a PR with a fix - assuming this is the desired behaviour

What did you expect to happen?

Given that operations like sel/isel don't drop the dimensions by default, I think it makes sense to keep the dimension by default here too.

Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!

import numpy as np

import xarray as xr

xr.show_versions()

base = xr.Dataset(
    {"elevation": (("lat",), np.array([0.0, 1.0, 2.0]))},
    coords={"time": [0], "lat": [0, 1, 2]},
)
weights = xr.DataArray([1.0, 1.0, 1.0], dims="lat", coords={"lat": [0, 1, 2]})

print("*" * 20, " base array ", "*" * 20)
print(base)

print("*" * 20, " weighted mean: base.weighted(weights).mean('lat')", "*" * 20)
print(base.weighted(weights).mean("lat"))

print("*" * 20, " regular mean: base.mean('lat')", "*" * 20)
print(base.mean("lat"))
Steps to reproduce

Should work for any other weighted reduction

MVCE confirmation
  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided minimal example and compare weighted reductions with the corresponding regular reductions. Trace the weighted mean and other weighted reduction entry points to find where orphaned dimensions are removed; done means weighted reductions preserve unaffected dimensions like the regular versions, with tests covering the example and other weighted reductions.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.