Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter Pandas Dataframe with or statement

I have a pandas dataframe that I want to to filter the dataframe using column ‘closed_date’, which contains dates. I am trying to filter so that either the value is null or the value is a date within the last year.

df = df[(df['closed_date']>dt.today()-td(days=365)) | (df['closed_date'].isnull)]

This filter is what I am trying to get work but fails and gets this error

Exception has occurred: AssertionError
unsupported operand type(s) for |: ‘bool’ and ‘method’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I break it apart and try each aspect on its own, I get the expected results of dates within the last year or null.

Variants I have tried:

df = df[(df['closed_date']>dt.today()-td(days=365)) or (df['closed_date'].isnull)]

Error:

Exception has occurred: ValueError
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

df = df[(df['closed_date']>dt.today()-td(days=365)) or (df['closed_date'].isnull())]

Error:

Exception has occurred: ValueError
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

>Solution :

I think you are missing the parentheses to call the method isnull. Try:

df = df[(df['closed_date'] > dt.today() - td(days=365)) | (df['closed_date'].isnull())]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading