When can I use a boolean DataArray for indexing?

Question:

I’m using an xarray DataArray object for boolean indexing. It works… sometimes. In the example below, it works for the large array but not for the small one:

In [12]: x = xarray.DataArray(numpy.arange(336*49).reshape(336,49))

In [13]: x.values[x==-1]
Out[13]: array([], dtype=int64)

In [14]: x = xarray.DataArray(numpy.arange(20*10).reshape(20, 10))

In [15]: x.values[x==-1]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-15-9340240dc777> in <module>()
----> 1 x.values[x==-1]

IndexError: too many indices for array

Is it supposed to be possible at all? Why does it work in one case but not in the other?

(Of course, the correct adaptation would be x.values[x.values==-1]. But I was quite puzzled by the observed behaviour.)

Asked By: gerrit

||

Answers:

This appears to be a bug in NumPy. .values is a NumPy array, and NumPy arrays don’t seem to properly support indexing with boolean array-like objects (like an xarray.DataArray): https://github.com/numpy/numpy/issues/9633

Answered By: shoyer