Stack Exchange
Calculating xarray xclim climate indices on a rolling window (rather than fixed window) basis
xclim climate indices (https://xclim.readthedocs.io/en/stable/indices.html) are calculated on time resampling frequencies such as start of each month. I want to calculate these indices on a rolling basis i.e. monthly index for a sliding window.
Here is an example of calculating heat wave index for monthly fixed-windows:

import numpy as np
import pandas as pd
import xarray as xr
import xclim.indices as xci

# generate some temperature data
T = xr.DataArray(np.random.normal(loc=300,scale=2,size=365), dims="time", 
                      coords={"time": pd.date_range(start='2000',periods=365, freq='d')})

ds=T.to_dataset(name='T')
ds['T'].attrs['units'] = 'K'

# calculate heat wave index
hwi = xci.heat_wave_index(ds.T, freq='MS')

And you can calculate e.g. mean on a rolling window as:
ds.rolling(time=30).mean()


It's not currently possible to do this with xclim. You can fill a feature request in github: https://github.com/Ouranosinc/xclim/issues/new/choose
I think it will be fairly difficult to have a generic implementation though.
In the meantime, if you are only interested in hwi index, you can cherry-pick xclim internal functions and make your own rolling_hwi
from xclim.core.units import convert_units_to
from xclim.indices.run_length import windowed_run_count

thresh = convert_units_to("30 degC", ds.T)
rolling_temp = ds.T.rolling(time=30).construct("roll_window")
inter = rolling_temp > thresh
res = windowed_run_count(inter, window=5, dim="roll_window") # window=5 means: discard any run that are shorter than 5 days

res.attrs["units"] = "d"

