reduce() got multiple values for keyword argument 'dim'
See original GitHub issueMCVE Code Sample
geo5=xr.open_dataset(dir_path+'500GPH/hgt.mon.mean.nc').sel(time=slice('1981-12-01','2010-02-01'), lat=slice(40.,-40.), lon=slice(140,290), level=500.)
### probably problems originate here
filenames = sorted(glob.glob(dir_path+'t2m/air.2m.gauss.*'))
t2m2= xr.open_mfdataset(filenames, concat_dim='time', combine='by_coords').sel(time=slice('1981-12-01','2010-02-28'),
lat=slice(40.,-40.), lon=slice(140,290)).resample(time='1M').mean(dim='time')
print(geo5)
<xarray.Dataset>
Dimensions: (lat: 33, lon: 61, time: 339)
Coordinates:
level float32 500.0
* lat (lat) float32 40.0 37.5 35.0 32.5 30.0 ... -32.5 -35.0 -37.5 -40.0
* lon (lon) float32 140.0 142.5 145.0 147.5 ... 282.5 285.0 287.5 290.0
* time (time) datetime64[ns] 1981-12-01 1982-01-01 ... 2010-02-01
Data variables:
hgt (time, lat, lon) float32 ...
Attributes:
description: Data from NCEP initialized reanalysis (4x/day). These a...
platform: Model
Conventions: COARDS
NCO: 20121012
history: Created by NOAA-CIRES Climate Diagnostics Center (SAC) fr...
title: monthly mean hgt from the NCEP Reanalysis
References: http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reana...
dataset_title: NCEP-NCAR Reanalysis 1
print(t2m2)
<xarray.Dataset>
Dimensions: (lat: 42, lon: 80, nbnds: 2, time: 339)
Coordinates:
* time (time) datetime64[ns] 1981-12-31 1982-01-31 ... 2010-02-28
* lon (lon) float32 140.625 142.5 144.375 ... 285.0 286.875 288.75
* lat (lat) float32 39.047 37.1422 35.2375 ... -37.1422 -39.047
Dimensions without coordinates: nbnds
Data variables:
air (time, lat, lon) float32 dask.array<chunksize=(1, 42, 80), meta=np.ndarray>
time_bnds (time, nbnds) float64 dask.array<chunksize=(1, 2), meta=np.ndarray>
geo5_res= geo5.where(geo5['time.season']=='DJF')
geo5_djf= geo5_res.rolling(min_periods=3, center=True, time=3).mean(dim='time').dropna('time')
t2m2_res = t2m2.where(t2m2['time.season']=='DJF')
t2m2_djf = t2m2_res.rolling(time=3, min_periods=3,center=True).mean(dim='time').dropna('time')
Expected Output
geo5_djf <xarray.Dataset> Dimensions: (lat: 33, lon: 61, time: 29) Coordinates: level float32 500.0
- lat (lat) float32 40.0 37.5 35.0 32.5 30.0 … -32.5 -35.0 -37.5 -40.0
- lon (lon) float32 140.0 142.5 145.0 147.5 … 282.5 285.0 287.5 290.0
- time (time) datetime64[ns] 1982-01-01 1983-01-01 … 2010-01-01 Data variables: hgt (time, lat, lon) float32 5347.3706 5347.1294 … 5696.4443
Problem Description
When I process the t2m2 to seasonal Dec-Jan-Feb mean, I encounter one very strange error. The error is associated with ‘dim’.
Personally, I guess the variable ‘t2m2’ is different from variable ‘geo5’ only because ‘t2m2’ originally is daily mean data, I processes them to be monthly data, as you can see in the code part.
Probably this leads to the error. But I am not able to tell why since the data structure is almost the same as the one of ‘geo5’. I apply exactly the same method from processing ‘geo5’ to ‘t2m2’. Wierd enough is the method works fine for ‘geo5’ but fails for ‘t2m2’.
Is any one can help with this super annoying error? The error seems like the mean(dim=‘time’) has been set implicitly and I wrote mean(dim=‘time’) again.
please find attached website for dealing with the rolling. http://xarray.pydata.org/en/stable/generated/xarray.DataArray.rolling.html
TypeError Traceback (most recent call last) <ipython-input-71-e94a33c1aec5> in <module> 15 t2m2_res = t2m2.where(t2m2[‘time.season’]==‘DJF’) 16 —> 17 t2m2_djf = t2m2_res.air.rolling(time=3, min_periods=3,center=True).mean(dim=‘time’).dropna(‘time’)
//anaconda3/lib/python3.7/site-packages/xarray/core/rolling.py in method(self, **kwargs) 127 def method(self, **kwargs): 128 return self._numpy_or_bottleneck_reduce( –> 129 array_agg_func, bottleneck_move_func, **kwargs 130 ) 131
//anaconda3/lib/python3.7/site-packages/xarray/core/rolling.py in _numpy_or_bottleneck_reduce(self, array_agg_func, bottleneck_move_func, **kwargs) 381 return self._bottleneck_reduce(bottleneck_move_func, **kwargs) 382 else: –> 383 return self.reduce(array_agg_func, **kwargs) 384 385
//anaconda3/lib/python3.7/site-packages/xarray/core/rolling.py in reduce(self, func, **kwargs) 297 rolling_dim = utils.get_temp_dimname(self.obj.dims, “_rolling_dim”) 298 windows = self.construct(rolling_dim) –> 299 result = windows.reduce(func, dim=rolling_dim, **kwargs) 300 301 # Find valid windows based on count.
TypeError: reduce() got multiple values for keyword argument ‘dim’
Output of xr.show_versions()
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (3 by maintainers)
Top GitHub Comments
You’ve asked it to create the rolling object along dimension
time
(.rolling(time=3, ...)
) so that’s the dimension the reduction operation acts on.I hope it does 😉 . I skipped a few characters typing out my response…
If you make it
rolling(...).mean()
instead ofrolling().mean(dim="time')
, it should work