question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

reduce() got multiple values for keyword argument 'dim'

See original GitHub issue

MCVE 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()

# Paste the output here xr.show_versions() here

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dcheriancommented, Nov 9, 2019

Could you please tell me where exactly did the rolling(…) set the mean dimension implicitly?

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.

Should raise an error on rolling()

I hope it does 😉 . I skipped a few characters typing out my response…

1reaction
dcheriancommented, Nov 9, 2019

The error seems like the mean(dim=‘time’) has been set implicitly and I wrote mean(dim=‘time’) again.

If you make it rolling(...).mean() instead of rolling().mean(dim="time'), it should work

Read more comments on GitHub >

github_iconTop Results From Across the Web

MPI Collective Reduce and Allreduce with MPI.MINLOC in ...
TypeError: reduce() got multiple values for keyword argument 'op' ... Comm.reduce() first and only after checking with it state that the ...
Read more >
TypeError: got multiple values for argument in Python
The Python "TypeError: got multiple values for argument" occurs when we overwrite the value of a positional argument with a keyword argument in...
Read more >
torch.sum — PyTorch 1.13 documentation
Returns the sum of each row of the input tensor in the given dimension dim . If dim is a list of dimensions,...
Read more >
dask.array.Array.map_blocks - Dask documentation
array((), dtype=np.int32) . **kwargs. Other keyword arguments to pass to function. Values must be constants (not dask.arrays).
Read more >
numpy.ufunc.reduce — NumPy v1.24 Manual
ufunc.reduce(array, axis=0, dtype=None, out=None, keepdims=False, ... use the initial keyword argument to initialize the reduction with a different value, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found