JarvisButler290
JarvisButler290

Reputation: 83

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'

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

Upvotes: 0

Views: 174

Answers (3)

confused_certainties
confused_certainties

Reputation: 589

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

Upvotes: 0

hd1
hd1

Reputation: 34677

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

Upvotes: 0

Zach Flanders
Zach Flanders

Reputation: 1314

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

Upvotes: 2

Related Questions