Xarray DataArray resample missing values result in zeros

Hi,

I have an xarray DataArray with dimensions time, y, and x. Time is in datetime64[ns] format with an hourly frequency. Part of the array contain missing values (np.nan), as they are not part of the study area. These are the same for every time-step. Now I like to resample this to daily grids using:

p = d[‘prec’].resample(time=‘1D’, skipna=True).sum()

However, all the cells that had np.nan in the original DataArray now have a value of zero after resampling. Why are these not kept at NaN? Changing skipna to False does not seem to make a difference.

Anyone any suggestions? Or is this a bug?

Cheers,
Wilco

If xarray is the same as pandas here, which I think it use, you want the min_count=1 keyword

min_count : int, default: None
    The required number of valid values to perform the operation. If
    fewer than min_count non-NA values are present the result will be
    NA. Only used if skipna is set to True or defaults to True for the
    array's dtype. New in version 0.10.8: Added with the default being
    None. Changed in version 0.17.0: if specified on an integer array
    and skipna=True, the result will be a float array.

There’s some ambiguity about whether the sum([]) should be 0 or NA. Most systems define it as 0.

1 Like

Perfect, that works like a charm. Thanks for this easy solution:

p = d.resample(time=‘1D’).sum(min_count=1)

Cheers.

Wilco