0

I need to calculate 5 year averaged data (not rolling average, but a calendar year one) from monthly mean or yearly mean time series data. After searching xarray documentation I cannot see a simple way to do it. Does anyone have a method for doing this type of averages?

thank you!

shoyer
  • 9,165
  • 1
  • 37
  • 55

1 Answers1

2

The easiest way to compute a 5 year average is to pass a custom frequency to resample(), e.g.,

In [24]: ds = xr.Dataset({'x': ('time', np.arange(1000))},
    ...:                 {'time': pd.date_range('2000-01', freq='1MS', periods=1000)})

In [28]: ds.resample('5AS', dim='time')
Out[28]:
<xarray.Dataset>
Dimensions:  (time: 17)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2005-01-01 2010-01-01 ...
Data variables:
    x        (time) float64 29.5 89.5 149.5 209.5 269.5 329.5 389.5 449.5 ...

Here "AS" indicates "annual, start".

shoyer
  • 9,165
  • 1
  • 37
  • 55
  • @user3207603: If you are concerned about proper weighting of the monthly means, this example may be of use to you: http://xarray.pydata.org/en/stable/examples/monthly-means.html – jhamman Oct 11 '17 at 16:48
  • Thank you both for your replies. I will try the 5AS option. – user3207603 Oct 13 '17 at 10:34