When I do running / rolling mean with weights in numpy, I e.g. do something like this:
data = np.random.random(100) # Example data...
weights = np.array([1,
The weighted-rolling-mean is not yet implemented in xarray.
The following does almost the same thing but it would be quite slow. I think the use of np.convolve is the current best choice.
def weighted_sum(x, axis):
weight = [1, 2, 1]
if x.shape[axis] == 3:
return np.sum(x * weight, axis=axis)
else:
return np.nan
da.rolling(dim_0=3, center=True).reduce(weighted_sum)
Currently, we are working to support more flexible (and faster) rolling operations. See https://github.com/pydata/xarray/pull/1837
EDIT:
With xarray=0.10.2, weighted rolling mean can be computed as follows,
weight = xr.DataArray([0.25, 0.5, 0.25], dims=['window'])
da.rolling(dim_0=3, center=True).construct('window').dot(weight)
where construct
method constructs a view of the rolling object, where the window dimension (named window
in the above example) is attatched to the last position.
inner product with the weight array gives the weighted sum along the window dimension.