$ curl cheat.sh/
#  When you say

 (a['x']==1) and (a['y']==10)

#  You are implicitly asking Python to convert `(a['x']==1)` and
#  `(a['y']==10)` to boolean values.
#  
#  NumPy arrays (of length greater than 1) and Pandas objects such as
#  Series do not have a boolean value -- in other words, they raise

 ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().

#  when used as a boolean value. That's because its [unclear when it
#  should be True or False][1]. Some users might assume they are True if
#  they have non-zero length, like a Python list. Others might desire for
#  it to be True only if **all** its elements are True. Others might want
#  it to be True if **any** of its elements are True.
#  
#  Because there are so many conflicting expectations, the designers of
#  NumPy and Pandas refuse to guess, and instead raise a ValueError.
#  
#  Instead, you must be explicit, by calling the `empty()`, `all()` or
#  `any()` method to indicate which behavior you desire.
#  
#  In this case, however, it looks like you do not want boolean
#  evaluation, you want **element-wise** logical-and. That is what the
#  `&` binary operator performs:

 (a['x']==1) & (a['y']==10)

#  returns a boolean array.
#  
#  ----------
#  
#  By the way, as [alexpmil
#  notes](https://stackoverflow.com/questions/21415661/logic-operator-
#  for-boolean-indexing-in-
#  pandas/21415990?noredirect=1comment77317569_21415990),
#  the parentheses are mandatory since `&` has a higher [operator precede
#  nce](https://docs.python.org/3/reference/expressions.htmloperator-
#  precedence) than `==`.
#  Without the parentheses, `a['x']==1 & a['y']==10` would be evaluated
#  as `a['x'] == (1 & a['y']) == 10` which would in turn be equivalent to
#  the chained comparison `(a['x'] == (1 & a['y'])) and ((1 & a['y']) ==
#  10)`. That is an expression of the form `Series and Series`.
#  The use of `and` with two Series would again trigger the same
#  `ValueError` as above. That's why the parentheses are mandatory.
#  
  #  [1]: http://pandas.pydata.org/pandas-docs/dev/gotchas.htmlusing-if-
#  truth-statements-with-pandas
#  
#  [unutbu] [so/q/21415661] [cc by-sa 3.0]

$
Follow @igor_chubin cheat.sh