Skip to content

reduce() by multiple dims on groupby object  #3402

Description

@tlogan2000

MCVE Code Sample

# Your code here
import xarray as xr
import numpy as np
url = 'https://data.nodc.noaa.gov/thredds/dodsC/GCOS/monthly_five_degree/19810101-NODC-L3_GHRSST-SSTblend-GLOB_HadSST2-Monthly_FiveDeg_DayNitAvg_19810101_20071231-v01.7-fv01.0.nc'

ds = xr.open_dataset(url, chunks=dict(time=12))
# reduce() directly on dataArray - THIS IS OK
ds.analysed_sst.reduce(np.percentile, dim=('lat','lon'), q=0.5) # ok

# Group by example
rr = ds.analysed_sst.rolling(min_periods=1, center=True, time=5).construct("window")

g = rr.groupby("time.dayofyear")
print(g.dims)
test1d = g.reduce(np.percentile, dim=('time'), q=0.5) # ok
testall = g.reduce(np.percentile, dim=xr.ALL_DIMS, q=0.5) # ok

# .reduce() w/ 2dims  on grouby obj not working 
test2d = g.reduce(np.nanpercentile, dim=('time','window'), q=0.5)

Expected Output

reduced output performed over multiple dimensions (but not xr.ALL_DIMS) on a groupby object

Problem Description

Using .reduce() on a groupby object is only successful when given a single dimensions or by using xr.ALL_DIMS. I wish to apply a reduce on a subset of dims (last line of code above) but gives folowing error:

Traceback (most recent call last):
  File "/home/travis/.PyCharmCE2019.2/config/scratches/scratch_20.py", line 13, in <module>
    test = g.reduce(np.percentile, dim=('time','window'), q=0.5)
  File "/home/travis/.conda/envs/Xarray/lib/python3.7/site-packages/xarray/core/groupby.py", line 800, in reduce
    % (dim, self.dims)
ValueError: cannot reduce over dimension ('time', 'window'). expected either xarray.ALL_DIMS to reduce over all dimensions or one or more of ('time', 'lat', 'lon', 'window').

Note: Using reduce() on a subset of dims directly on a xr.DataArray seems fine (line 7).

Output of xr.show_versions()

Details INSTALLED VERSIONS ------------------ commit: None python: 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0] python-bits: 64 OS: Linux OS-release: 4.15.0-65-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_CA.UTF-8 LOCALE: en_CA.UTF-8 libhdf5: 1.10.2 libnetcdf: 4.6.3 xarray: 0.14.0 pandas: 0.25.1 numpy: 1.17.2 scipy: 1.3.1 netCDF4: 1.5.2 pydap: None h5netcdf: None h5py: None Nio: None zarr: None cftime: 1.0.3.4 nc_time_axis: None PseudoNetCDF: None rasterio: None cfgrib: None iris: None bottleneck: 1.2.1 dask: 2.4.0 distributed: 2.4.0 matplotlib: 3.1.1 cartopy: None seaborn: None numbagg: None setuptools: 41.0.1 pip: 19.2.2 conda: None pytest: 5.0.1 IPython: None sphinx: None

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions