Take a "nanmean" in xarray

Viewed 1815

I know that I can use the numpy nanmean function to take the mean of a numpy array, while ignoring NaN values. Is there an analogous way to accomplish this with xarray? I will give an example...

numpy_array=[1,2,3,4,float('nan'),5]

np.mean(numpy_array)
> NaN

np.nanmean(numpy_array)
> 3.0

In xarray, I can do

xarray_example.mean(dim='dimension')

How can I change this to a nanmean?

Thanks.

1 Answers

According to the xarray docs for the mean function, you can set the skipna parameter as True (it will skip missing float values by default). So:

xarray_example.mean(dim='dimension', skipna=True)
Related