Reputation: 269
Say I have
>>> arr = np.array([True, True, False], dtype=bool)
Is it possible to call something like
>>> arr.flip_boolean_array_by_index(2)
[True, True, True]
Upvotes: 0
Views: 1206
Reputation: 638
I found that ^= 1
on a dytpe=numpy.bool_
array gave a UFuncTypeError (can't cast from dtype int64 to dtype bool). However, ^= True
works, e.g. arr[idx] ^= True
. This was in numpy 1.26.3.
Upvotes: 0
Reputation: 64318
You can use the bitwise-negation operator ~
, or bitwise-xor (^
) with 1.
arr[idx] = ~arr[idx]
or
arr[idx] ^= 1
idx
can be an index, a slice, a "fancy" index, etc.
Upvotes: 2