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], ], 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
my_mask = [True, True, True, True] arr[my_mask] # array([[list([1, 2]), list([3, 4, 5]), list([6, 7, 8, 9]), list()]], 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.
instead of using:
arr[True, False, True, True]
using this (when you want to pass
array of mask):
arr[[True, False, True, True]]