Problem:
I'd like to resample a xarray dataset e.g. the sum or mean with each resulting value being nan when at least one of the input values was nan. With pandas I can easily apply an own mean,sum etc. function giving me my preferred nan treatment. xarray also allows resample.apply(own_func) but I have problems defining the own func.
Example (from xarray's documentation):
dat=np.linspace(0, 11, 12)
dat[2]=np.nan
da = xr.DataArray(dat,
coords=[pd.date_range('15/12/1999',
periods=12,
freq=pd.DateOffset(months=1))],
dims='time')
da.resample(time="QS-DEC").sum()
What I get:
<xarray.DataArray (time: 4)>
array([ 1., 12., 21., 30.])
Coordinates:
* time (time) datetime64[ns] 1999-12-01 2000-03-01 2000-06-01 2000-09-01
@JulianGiles answer:
da.resample(time="QS-DEC",skipna=False).mean()
<xarray.DataArray (time: 4)>
array([ 0.5, 4. , 7. , 10. ])
Coordinates:
* time (time) datetime64[ns] 1999-12-01 2000-03-01 2000-06-01 2000-09-01
What I want:
<xarray.DataArray (time: 4)>
array([ 1., NAN, 21., 30.])
Coordinates:
* time (time) datetime64[ns] 1999-12-01 2000-03-01 2000-06-01 2000-09-01