-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Labels
Description
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).