Skip to content
Advertisement

compute mean from several years using xarray

I want to compute the mean for an xarray dataset that contains daily temperature data from 2015-2020. Here is what I am doing currently:

ds.groupby('time.year').mean('time')

However, this gives me annual mean for each year separately, whereas I want the mean for all the years. How do I fix this?

Advertisement

Answer

If you’re looking for the mean across the entire time dimension, xarray’s mean function (no group by) is what you’re looking for:

ds.mean(dim='time')

If your data has a variable number of observations for each year (including if it contains leap years) and you want to make sure each year is given equal weight regardless of the number of observations per year, you could do a groupby-mean to get the annual average and then calculate the cross-year mean:

ds.groupby('time.year').mean('time').mean(dim='time')

resample would also work:

ds.resample(time='Y').mean('time').mean(dim='time')
Advertisement