pydata / pydata/xarray

Unexpected Dataset aggregation behavior when weighting

Open
#8,583 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What happened?

When aggregating a dataset over specified dimensions I don't expect variables which don't have those dimensions to be aggregated.

What did you expect to happen?

When a weighting is applied to the aggregation, variables which do not have the aggregation dimensions are nevertheless aggregated. Presumably because the weights get broadcast across those variables. Perhaps this is the intended behavior but it seems surprising to me and should at least be documented I think.

Minimal Complete Verifiable Example
import xarray as xr
import numpy as np

var1 = np.ones((2, 2, 3))

var2 = np.ones((3))

lon = np.arange(4).reshape(2, 2)
lat = np.arange(4).reshape(2, 2)

ds = xr.Dataset(
    {
        "temperature": (["x", "y", "time"], var1),
        "precipitation": (["time"], var2),
    },
    coords={
        "lon": (["x", "y"], lon),
        "lat": (["x", "y"], lat),
        "time": np.arange(3),
    },
)

print(ds.sum(['x', 'y']))
# Precipitation (with no x or y dimension) is not summed over, leading to values [1. 1. 1.]

print(ds.weighted(xr.ones_like(ds['temperature'])).sum(['x', 'y']))
# Precipitation is now summed over, leading to values [4. 4. 4.]
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.
Relevant log output

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:38:11)
[Clang 14.0.6 ]
python-bits: 64
OS: Darwin
OS-release: 23.1.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: None
LOCALE: (None, 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.9.1

xarray: 2023.3.0
pandas: 1.5.3
numpy: 1.23.5
scipy: 1.10.1
netCDF4: 1.6.3
pydap: None
h5netcdf: None
h5py: 3.8.0
Nio: None
zarr: 2.14.2
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.3.6
cfgrib: None
iris: 3.4.1
bottleneck: None
dask: 2023.3.2
distributed: 2023.3.2.1
matplotlib: 3.7.1
cartopy: 0.21.1
seaborn: 0.12.2
numbagg: None
fsspec: 2023.10.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 67.6.1
pip: 23.0.1
conda: None
pytest: None
mypy: None
IPython: 8.12.0
sphinx: None

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

Run the Python MVCE to compare Dataset.sum(['x', 'y']) with weighted(...).sum(['x', 'y']). Trace the Dataset aggregation and weighting entry points to determine whether broadcasting weights to variables without the dimensions is intended. Done means the behavior is resolved and its expected semantics are documented or covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.