Search code examples
pythonpandaspython-xarray

Why is there an error 'TypeError: Grouper.__init__() got an unexpected keyword argument 'base''?


Versions needed to be known :

  • Jupyter Notebook : 6.5.4

  • Python : 3.11.3

  • Pandas : 2.2.2

  • xarray : 2022.11.0

I am doing an assignment by using a precipitation dataset from http://research.jisao.washington.edu/data_sets/widmann/ to perform sensor selection (where should you place sensor to best predict rain).

First, I opened the file and read it, using the code below :

import xarray as xr
import pandas as pd

nc_file_path = './pnwrain.50km.daily.4994.nc' 
dataset = xr.open_dataset(nc_file_path,decode_times=False)
dataset 

Output :

image1

Then, I convert the time variable into datetime, using the code below :

ref_date = pd.to_datetime('1949-01-01')
# Convert 'time' variable to datetimes
dataset['time'] = ref_date + pd.to_timedelta(dataset['time'], unit='D') 

After that, I calculate the monthly totals in months, using the code below :

monthly_totals = dataset['data'].resample(time='ME').sum()

Output :

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 monthly_totals = dataset['data'].resample(time='ME').sum()
      3 print("Monthly totals: ", monthly_totals)

File ~\anaconda3\Lib\site-packages\xarray\core\dataarray.py:6636, in DataArray.resample(self, indexer, skipna, closed, label, base, keep_attrs, loffset, restore_coord_dims, **indexer_kwargs)
   6540 """Returns a Resample object for performing resampling operations.
   6541 
   6542 Handles both downsampling and upsampling. The resampled
   (...)
   6632 .. [1] http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
   6633 """
   6634 from .resample import DataArrayResample
-> 6636 return self._resample(
   6637     resample_cls=DataArrayResample,
   6638     indexer=indexer,
   6639     skipna=skipna,
   6640     closed=closed,
   6641     label=label,
   6642     base=base,
   6643     keep_attrs=keep_attrs,
   6644     loffset=loffset,
   6645     restore_coord_dims=restore_coord_dims,
   6646     **indexer_kwargs,
   6647 )

File ~\anaconda3\Lib\site-packages\xarray\core\common.py:965, in DataWithCoords._resample(self, resample_cls, indexer, skipna, closed, label, base, keep_attrs, loffset, restore_coord_dims, **indexer_kwargs)
    963         grouper = CFTimeGrouper(freq, closed, label, base, loffset)
    964     else:
--> 965         grouper = pd.Grouper(
    966             freq=freq, closed=closed, label=label, base=base, loffset=loffset
    967         )
    968 group = DataArray(
    969     dim_coord, coords=dim_coord.coords, dims=dim_coord.dims, name=RESAMPLE_DIM
    970 )
    971 return resample_cls(
    972     self,
    973     group=group,
   (...)
    977     restore_coord_dims=restore_coord_dims,
    978 )

File ~\anaconda3\Lib\site-packages\pandas\core\resample.py:2208, in TimeGrouper.__init__(self, obj, freq, key, closed, label, how, axis, fill_method, limit, kind, convention, origin, offset, group_keys, **kwargs)
   2205 # always sort time groupers
   2206 kwargs["sort"] = True
-> 2208 super().__init__(freq=freq, key=key, axis=axis, **kwargs)

TypeError: Grouper.__init__() got an unexpected keyword argument 'base'

Do anyone know why this error is encountered?


Solution

  • I would try to update Xarray. Pandas 2.2.2 is brand new and Xarray 2022.11.0 is ~2 yrs old. I suspect there is a mismatch there.