1

I'm resampling a daily xarray dataset into monthly values. Is there a straightforward way to output NaN when for example only 50 percent of the days have valid data?

In the moment I'm using the xarray standard function which will output a monthly average regardless of the number of valid days: ds.resample(time='1MS').mean(dim='time')

Val
  • 6,585
  • 5
  • 22
  • 52
dr226
  • 11
  • 1
  • you could count the number of `NaN`s and set pixels that have a count > 50% to `NaN` themselves. – Val Oct 19 '22 at 16:29

1 Answers1

1

You can use DataArrayResample.map to apply a custom reduction while resampling:

def nan50_mean(da):
    return da.mean(dim='time').where(
        da.notnull().sum(dim='time') >= len(da.time) * 0.5
    )

ds.resample(time='1MS').map(nan50_mean)
Michael Delgado
  • 13,789
  • 3
  • 29
  • 54