Search code examples
pythonpandaspython-xarray

How to perform resampling in xarray without pandas Grouper error?


I am trying to resample an xarray DataArray to get the mean values of the array at N-second intervals. My DataArray has two co-ordinates, time and latitude (LAT_GIN), with one dimension: time. A minimal version of this with 100 time points is below:

# Create a time coordinate
start_time = pd.Timestamp("2023-09-25 12:34:56")
time_values = pd.date_range(start_time, periods=100, freq="1S")
lat_values = np.random.rand(100).astype(np.float32)
da = xr.DataArray(lat_values, coords={'time': time_values}, dims=['time'])
da.coords['LAT_GIN'] = xr.Variable(('time',), lat_values)

To resample every, e.g. 20 seconds, I am using the line:

da.resample(time='20S').mean()

The output I am expecting is a resampled DataArray with five values (100/20), where the LAT_GIN co-ordinate has also been averaged over each of the ten sample periods.

Instead, I get the following error:

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

Solution

  • This turned out to be a bug in Pandas 2.1.0. It worked fine in Pandas 1.5.3.