Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When can I use a boolean DataArray for indexing?

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.)

like image 950
gerrit Avatar asked Feb 05 '26 10:02

gerrit


1 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

like image 127
shoyer Avatar answered Feb 07 '26 23:02

shoyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!