Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

You must login to add post.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Passionable Logo Passionable Logo
Sign InSign Up

Passionable

Passionable Navigation

  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions
Home/ Questions/Q 5808
In Process
Alek Richter
  • 0
Alek RichterEnlightened
Asked: December 17, 20212021-12-17T05:38:37+00:00 2021-12-17T05:38:37+00:00

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

  • 0

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)]
  • 2 2 Answers
  • 15 Views
  • 0 Followers
  • 0
    • Report
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Alek Richter Enlightened
    2021-12-17T05:39:15+00:00Added an answer on December 17, 2021 at 5:39 am

    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
      

    If you’re using the operators then make sure you set your parenthesis correctly because of the operator precedence.

    There are several logical numpy functions which should work on pandas.Series.


    The alternatives mentioned in the Exception are more suited if you encountered it when doing if or while. I’ll shortly explain each of these:

    • If you want to check if your Series is empty:

      >>> x = pd.Series([])
      >>> x.empty
      True
      >>> x = pd.Series([1])
      >>> x.empty
      False
      

      Python normally interprets the length of containers (like list, tuple, …) as truth-value if it has no explicit boolean interpretation. So if you want the python-like check, you could do: if x.size or if not x.empty instead of if x.

    • If your Series contains one and only one boolean value:

      >>> x = pd.Series([100])
      >>> (x > 50).bool()
      True
      >>> (x < 50).bool()
      False
      
    • If you want to check the first and only item of your Series (like .bool() but works even for not boolean contents):

      >>> x = pd.Series([100])
      >>> x.item()
      100
      
    • If you want to check if all or any item is not-zero, not-empty or not-False:

      >>> x = pd.Series([0, 1, 2])
      >>> x.all()   # because one element is zero
      False
      >>> x.any()   # because one (or more) elements are non-zero
      True
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Alek Richter Enlightened
    2021-12-17T05:39:43+00:00Added an answer on December 17, 2021 at 5:39 am

    Well pandas use bitwise & | and each condition should be wrapped in a ()

    For example following works

    data_query = data[(data['year'] >= 2005) & (data['year'] <= 2010)]
    

    But the same query without proper brackets does not

    data_query = data[(data['year'] >= 2005 & data['year'] <= 2010)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 4k
  • Best Answers 0
  • Users 2
  • Popular
  • Answers
  • Alek Richter

    How do I update/upgrade pip itself from inside my virtual ...

    • 2 Answers
  • Alek Richter

    Truth value of a Series is ambiguous. Use a.empty, a.bool(), ...

    • 2 Answers
  • Alek Richter

    What is a NullPointerException, and how do I fix it?

    • 2 Answers
  • Alek Richter
    Alek Richter added an answer Pandas DataFrame columns are Pandas Series when you pull them… January 13, 2022 at 2:21 pm
  • Alek Richter
    Alek Richter added an answer The handshake failure could have occurred due to various reasons:… January 13, 2022 at 2:19 pm
  • Alek Richter
    Alek Richter added an answer Mac OS X doesn't have apt-get. There is a package… January 13, 2022 at 2:18 pm

Top Members

Alek Richter

Alek Richter

  • 4k Questions
  • 1k Points
Enlightened

Trending Tags

questin question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • New Questions
  • Trending Questions
  • Must read Questions
  • Hot Questions

© 2021 Passionable. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.