Suppose I have two NumPy arrays:
>>> import numpy as np
>>> a = np.arange(2)
>>> b = np.arange(2)
They can be compared without raising an exception, though the result is, as expected, not a single value:
>>> a > b
array([False, False], dtype=bool)
However, putting them in a tuple comparison that requires comparing them does raise an exception:
>>> (1, a) > (1, b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
A similar happens with Pandas Series
objects; in that case, __nonzero__
is called. The Python documentation says that method is for converting to bool
, which seems not relevant here.
There is another question about how to accomplish the comparison correctly.
But, my question is: Why does this happen? How do booleans get involved? Why is there not a more logical exception about not being able to compare the objects?
This is Python 3.4.