Skip to content Skip to sidebar Skip to footer

Xarray Annual Grouping Across Years

I am computing annual means of my data with: sst_ANN = ds['sst'].groupby(ds['time.year']).mean(dim='time') which is surely elegant. Now I want to do the same thing, except compute

Solution 1:

You probably should be using xarray's resample method instead of groupby (they are similar in this case but resample is slightly easier to manipulate to do what you want).

For xarray 0.10.0 and later, this would look like:

sst_ANN = ds['sst'].resample(time='AS-JUN').mean('time')

For older versions of xarray, it would look like:

sst_ANN = ds['sst'].resample(freq='AS-JUN', dim='time', how='mean')

In both cases, we use an "anchored offset". The AS-JUN translates to "annual, start of period, beginning in June".

Post a Comment for "Xarray Annual Grouping Across Years"