Hi all. I'm working with some monthly data formatted as an xarray.DataArray
, and I'm trying to compute three month averages for the seasons DJF, MAM, JJA, and SON. The tricky part is that I'd like to make these averages weighted based on the number of days in each month. So far, I have the following dummy data and code which computes seasonal averages from the monthly data, but it isn't weighted.
time = pd.date_range('2020-01-01', '2020-12-31', freq='M') temp_data = np.arange(len(time)) da = xr.DataArray(data=temp_data, coords=dict(time=time,), dims=['time'], attrs=dict(description='Dummy Data')) avgs = da.rolling({'time': 3}, center=True)\ .construct('window_dim', stride=3)\ .mean('window_dim') print(avgs)
Output
<xarray.DataArray (time: 4)> array([0.5, 3. , 6. , 9. ]) Coordinates: * time (time) datetime64[ns] 2020-01-31 2020-04-30 2020-07-31 2020-10-31 Attributes: description: Dummy Data
I've tried following the last example in the rolling window operations section of the xarray documentation, but I haven't gotten anywhere. Does anyone know how of xarray functions that can help me do what I want? Thanks in advance!
This problem is actually featured in a groupby
example in the xarray docs gallery! link
Huh for some reason I never found this page. Thanks! I think it's exactly what I need.
@Abby Jaye
Last updated: May 16 2025 at 17:14 UTC