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
What happened?
Weighted reductions drop orphaned dimensions - unlike their non-weighted versions. See the MRE, but TLDR;
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/iseldon't drop the dimensions by default, I think it makes sense to keep the dimension by default here too.Minimal Complete Verifiable Example
Steps to reproduce
Should work for any other weighted reduction
MVCE confirmation