QBoard » Artificial Intelligence & ML » AI and ML - Python » Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

  • Having issue filtering my result dataframe with an or condition. I want my result df to extract all column var values that are above 0.25 and below -0.25.

    This logic below gives me an ambiguous truth value however it work when I split this filtering in two separate operations. What is happening here? not sure where to use the suggested a.empty(), a.bool(), a.item(),a.any() or a.all().
    result = result[(result['var'] > 0.25) or (result['var'] < -0.25)]
    ​
      August 6, 2021 10:02 PM IST
    0
  • The or and and python statements require truth-values. For pandas these are considered ambiguous so you should use "bitwise" | (or) or & (and) operations:

    result = result[(result['var']>0.25) | (result['var']<-0.25)]
    ​


    These are overloaded for these kind of datastructures to yield the element-wise or (or and).

    Just to add some more explanation to this statement:

    The exception is thrown when you want to get the bool of a pandas.Series:

    >>> import pandas as pd
    >>> x = pd.Series([1])
    >>> bool(x)
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().​


    What you hit was a place where the operator implicitly converted the operands to bool (you used or but it also happens for and, if and while):

    >>> x or x
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
    >>> x and x
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
    >>> if x:
    ...     print('fun')
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
    >>> while x:
    ...     print('fun')
    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


    Besides these 4 statements there are several python functions that hide some bool calls (like any, all, filter, ...) these are normally not problematic with pandas.Series but for completeness I wanted to mention these.

    In your case the exception isn't really helpful, because it doesn't mention the right alternatives. For and and or you can use (if you want element-wise comparisons):

    numpy.logical_or:

    >>> import numpy as np
    >>> np.logical_or(x, y)


    or simply the | operator:

    >>> x | y


    numpy.logical_and :

    >>> np.logical_and(x, y)

     

    or simply the & operator:

    >>> x & y



    This post was edited by Samar Patil at August 7, 2021 1:11 PM IST
      August 7, 2021 1:10 PM IST
    0
  • Or, alternatively, you could use Operator module. More detailed information is here Python docs

    import operator
    import numpy as np
    import pandas as pd
    np.random.seed(0)
    df = pd.DataFrame(np.random.randn(5,3), columns=list('ABC'))
    df.loc[operator.or_(df.C > 0.25, df.C < -0.25)]
    
              A         B         C
    0  1.764052  0.400157  0.978738
    1  2.240893  1.867558 -0.977278
    3  0.410599  0.144044  1.454274
    4  0.761038  0.121675  0.4438
      August 7, 2021 11:48 PM IST
    0
  • One minor thing, which wasted my time.

    Put the conditions(if comparing using " = ", " != ") in parenthesis, failing to do so also raises this exception. This will work

    df[(some condition) conditional operator (some conditions)]
    

    This will not

    df[some condition conditional-operator some condition]
    
      November 24, 2021 11:56 AM IST
    0