pydata / pydata/xarray

xarray.DataArray.weighted performs unweighted mean if dimension names differ without any warning

Open
#6,952 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What happened?

dataarray.weighted(weights).mean() performs an unweighted mean if dimensions of weights and to-be-weighted-array differ. That is ok, but a warning message informing of it would be very helpful, as currently the mean is just performed and the user may assume that a weighted mean happenned.

What did you expect to happen?

I would expect to get a warning when performing the mean, knowing that it is not using the weights that it got assigned.

Minimal Complete Verifiable Example
import xarray as xr
import numpy as np
test_array = xr.DataArray(np.array([0,1,2]),
                     dims=('dim1'),
            )
weights = xr.DataArray(np.array([1,50,100]),
                     dims=('dim1'),
            )
weights2 = xr.DataArray(np.array([1,50,100]),
                     dims=('dim2'),
            )
# apply weights
test_array_weighted = test_array.weighted(weights)
#now apply weights with different dimension name
test_array_weighted2 = test_array.weighted(weights2)
# compute weighted mean
if (test_array_weighted.mean() != test_array_weighted2.mean()):
    print ('Different means')
    print ('test_array_weighted.mean=',test_array_weighted.mean())
    print ('test_array_weighted2.mean=',test_array_weighted2.mean())
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.
Relevant log output

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.9.9 | packaged by conda-forge | (main, Dec 20 2021, 02:41:03)
[GCC 9.4.0]
python-bits: 64
OS: Linux
OS-release: 4.18.0-305.25.1.el8_4.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.1
libnetcdf: 4.8.1

xarray: 2022.3.0
pandas: 1.4.2
numpy: 1.20.3
scipy: 1.8.1
netCDF4: 1.6.0
pydap: None
h5netcdf: 1.0.0
h5py: 3.7.0
Nio: None
zarr: 2.11.3
cftime: 1.6.0
nc_time_axis: 1.4.1
PseudoNetCDF: None
rasterio: 1.2.10
cfgrib: 0.9.8.5
iris: 3.2.1
bottleneck: 1.3.4
dask: 2022.6.0
distributed: 2022.6.0
matplotlib: 3.5.2
cartopy: 0.20.2
seaborn: 0.11.2
numbagg: None
fsspec: 2022.5.0
cupy: None
pint: 0.19.2
sparse: 0.13.0
setuptools: 62.3.4
pip: 22.1.2
conda: 4.13.0
pytest: 7.1.2
IPython: 8.4.0
sphinx: 5.0.1

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 reproducing the Minimal Complete Verifiable Example with xarray.DataArray.weighted(), comparing weights with matching and differing dimension names. Trace the weighted mean behavior and add coverage for the differing-dimension case; done means the unweighted fallback emits a warning while the matching-dimension case remains unchanged.

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
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.