I believe this is what's going on:
When you use a comparison operator on a NumPy array, it returns an array of booleans with the same shape as the input array. Each value is the result of bool(x[i] == 4)
if you have a list, you get
>>> bool([4, 4, 4] == 4)
False
with a numpy array, you get:
>>> bool(np.array([4, 4, 4]) == 4)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-65795746028a> in <module>()
----> 1 bool(np.array([4, 4, 4]) == 4)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I would have thought that numpy would propagate this error through, but it doesn't. For some reason it decides to evaluate the entire expression as False. It might be worth bringing this up on the numpy issue tracker.
As for solving your problem... is there any way you can use lists rather than arrays within your object array? I can't think of another way to get around it.
Edit: I submitted an issue at https://github.com/numpy/numpy/issues/5016
Edit2: According to numpy developers, the current version of numpy raises a warning in this situation, and a future version will raise a ValueError when the elementwise comparison fails.
Answer from jakevdp on Stack OverflowI believe this is what's going on:
When you use a comparison operator on a NumPy array, it returns an array of booleans with the same shape as the input array. Each value is the result of bool(x[i] == 4)
if you have a list, you get
>>> bool([4, 4, 4] == 4)
False
with a numpy array, you get:
>>> bool(np.array([4, 4, 4]) == 4)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-17-65795746028a> in <module>()
----> 1 bool(np.array([4, 4, 4]) == 4)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I would have thought that numpy would propagate this error through, but it doesn't. For some reason it decides to evaluate the entire expression as False. It might be worth bringing this up on the numpy issue tracker.
As for solving your problem... is there any way you can use lists rather than arrays within your object array? I can't think of another way to get around it.
Edit: I submitted an issue at https://github.com/numpy/numpy/issues/5016
Edit2: According to numpy developers, the current version of numpy raises a warning in this situation, and a future version will raise a ValueError when the elementwise comparison fails.
Somehow the nested np.array disturb the element-wise condition == 4. Nesting normal lists works just fine:
import numpy as np
foo = np.array([[0, 2, 3], [0, 2, 3], [4, 4], 4], dtype=object)
np.where(foo == 4)
But I'm not sure why this makes the difference.