python-xarrayargmax

How to skip nan slices in xarray argmax() function?


I have a large multidimensional xarray data array. I am trying to apply xarray.argmax() to each column but am unable to since some of the "slices" are all-NaN.

Here is a smaller, reproducible version of my problem:

import xarray
da = xarray.DataArray(np.array([[0,2,3],[np.nan,np.nan,np.nan],[1,5,3]]))
da.argmax(axis =1)

I get the error: ValueError: All-NaN slice encountered

All I want to do is completely ignore the All-NaN slices so that this example would output [2,1]. Any help would be most appreciated.


Solution

  • You should drop the nans beforehand, like so: enter image description here