pydata / pydata/xarray

don't apply `weighted`, `groupby`, etc. to `DataArray` without `dims`?

Open
#7,027 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What is your issue?

Applying e.g. ds.weighted(weights).mean() applies the operation over all DataArray objects - even if they don't have the dimensions over which it is applied (or is a scalar variable). I don't think this is wanted.

import xarray as xr

air = xr.tutorial.open_dataset("air_temperature")
air.attrs = {}

# add variable without dims
air["foo"] = 5

print("resample")
print(air.resample(time="MS").mean(dim="time").foo.dims)

print("groupby")
print(air.groupby("time.year").mean(dim="time").foo.dims)

print("weighted")
print(air.weighted(weights=air.time.dt.year).mean("lat").foo.dims)

print("where")
print(air.where(air.air > 5).foo.dims)

Results

resample
('time',)
groupby
('year',)
weighted
('time',)

Related #6952 - I am sure there are other issues, but couldn't find them quickly...

rolling and coarsen don't seem to do this.

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 reproduction against the Dataset operations resample, groupby, weighted, and where, checking the dimensions of the scalar foo variable. Compare these results with rolling and coarsen, then determine the consistent expected behavior for variables without the requested dimensions. Done means the affected operations no longer add those dimensions to scalar or otherwise unrelated DataArrays, with regression coverage for the demonstrated cases.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.