Reputation: 2151
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.
Upvotes: 0
Views: 719
Reputation: 40668
You should call arr[my_mask]
instead of arr[True, False, True, True]
.
The reason is that by indexing with True
you are adding a dimension,
>>> arr[True]
array([[list([1, 2]), list([3, 4, 5]), list([6, 7, 8, 9]), list([10])]],
dtype=object)
but then mask the second axis (formerly the first axis) with False
i.e. returning no elements.
Upvotes: 1
Reputation: 24049
instead of using:
arr[True, False, True, True]
using this (when you want to pass mask
, pass array of mask
):
arr[[True, False, True, True]]
Upvotes: 1