Home > OS >  Filter Pandas Dataframe with or statement
Filter Pandas Dataframe with or statement

Time:09-22

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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