I am creating an ndarray from ragged nested sequences, like this:
import numpy as np
arr = np.array([[1,2], [3,4,5], [6,7,8,9], [10]], dtype=object)
In a next step, I want to use a "boolean mask" of the same length as arr to get certain elements.
Passing a mask of all True works:
my_mask = [True, True, True, True]
arr[my_mask]
# array([[list([1, 2]), list([3, 4, 5]), list([6, 7, 8, 9]), list([10])]],
dtype=object)
However, other masks don't seem to work:
my_mask = [True, False, True, True]
arr[my_mask]
# array([], shape=(0, 4), dtype=object)
Why does the above result in an empty array?
UPDATE: In the example above I wrote arr[my_mask], but the error I got locally was actually obtained via arr[True, False, True, True], which should rather be arr[[True, False, True, True]], note the double brackets. Thanks to @Ivan and @user1740577. As such, this is not unexpected behavior, but rather a user mistake during indexing.
array([list([1, 2]), list([6, 7, 8, 9]), list([10])], dtype=object)arr[my_mask]or doingarr[True, True, True, True]directly?