Skip to content

Dataset.mean changes variables without specified dimension #4885

@dschwoerer

Description

@dschwoerer

What happened:
If I apply mean(dim='time') on a dataset, variables without that dimension are changed.

What you expected to happen:
Variables without the dimension are not changed.

Minimal Complete Verifiable Example:

import xarray as xr

ds = xr.Dataset()
ds["pos"] = [1, 2, 3]
ds["data"] = ("pos", "time"), [[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]
ds["var"] = "pos", [2, 3, 4]
print(ds.mean(dim="time"))

Anything else we need to know?:
That makes it unnecessarily slow, as variables without that dimensions wouldn't need to be read from disk.
It is easy enough to work around:

ds2 = ds.copy()
for k in ds:
    if "time" in ds[k].dims:
        ds2[k] = ds[k].mean(dim="time")

However I cannot see why dataset should change the variables without the specified dim.

Environment:

Output of xr.show_versions() INSTALLED VERSIONS ------------------

commit: None
python: 3.9.1 (default, Jan 20 2021, 00:00:00)
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)]
python-bits: 64
OS: Linux
OS-release: 5.10.13-200.fc33.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.10.6
libnetcdf: 4.7.3

xarray: 0.16.2
pandas: 1.0.5
numpy: 1.19.4
scipy: 1.5.2
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.1.3
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.2.1
dask: None
distributed: None
matplotlib: 3.3.4
cartopy: None
seaborn: None
numbagg: None
pint: 0.13
setuptools: 49.1.3
pip: 20.2.2
conda: None
pytest: 6.0.2
IPython: 7.18.1
sphinx: 3.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions