Filter Pandas Dataframe with or statement

Question:

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’

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().

Asked By: JarvisButler290

||

Answers:

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())]
Answered By: Zach Flanders

isnull is a function. As such, you need to call it as df = df[(df['closed_date'] > dt.today() - td(days=365)) | (df['closed_date'].isnull())].

Answered By: hd1

Your first method is missing the parentheses at the end of isnull

df = df[(df['closed_date']>dt.today()-td(days=365)) | (df['closed_date'].isnull())]
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.