Reputation: 7490
I have a numpy array as follows:
array([ True, True, True, True, True, False, True, True, False,
True, False, True, True, True, True, True, True, False,
False, False, False, False, True, True, False, False, False,
True, True, True, True, True, True, True, False, True,
True, True, True, False, True, True, False, False, True,
True, True, False, True, True, True, False],
I want to get the indices of all the True elements. There is no get_loc method in numpy like Pandas Series and similarly no index method like a list. I don't want to convert it into a list and then use .index.
Any idea?
Upvotes: 5
Views: 243
Reputation: 16134
The np.ix_
way seems to be the slowest.
In [846]: % timeit a.nonzero()
1000000 loops, best of 3: 707 ns per loop
In [845]: % timeit np.where(a)
1000000 loops, best of 3: 883 ns per loop
In [849]: %timeit np.ix_(a==True)
100000 loops, best of 3: 9.21 µs per loop
Upvotes: 0
Reputation: 393963
To do this in pandas:
In [255]:
s[s==True].index
Out[255]:
Int64Index([0, 1, 2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 22, 23, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 44, 45, 46, 48, 49, 50], dtype='int64')
Update
Actually you can use the fact that the values are already boolean values to mask the series:
In [256]:
s[s].index
Out[256]:
Int64Index([0, 1, 2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 22, 23, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 44, 45, 46, 48, 49, 50], dtype='int64')
Similarly for numpy arrays you can use the boolean values to mask the array and get the index values using np.where
:
In [261]:
np.where(a)
Out[261]:
(array([ 0, 1, 2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 22, 23, 27,
28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 44, 45, 46, 48, 49,
50], dtype=int64),)
Upvotes: 0
Reputation: 362557
Use ndarray.nonzero:
>>> a.nonzero()
(array([ 0, 1, 2, 3, 4, 6, 7, 9, 11, 12, 13, 14, 15, 16, 22, 23, 27,
28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 44, 45, 46, 48, 49,
50]),)
Upvotes: 3