Reputation:
I have a dataframe with a Date column and I would like to compare those dates with today's date inside an IF statement.
My dataframes looks like this:
Date Asset
2020-01-01 Computers
2018-02-01 Apartments
2017-09-03 Houses
2015-02-02 Boats
I am using the following code to convert to datetime:
df['Date'] = pd.to_datetime(df['Date'],format='%d%m%Y')
and to compare today's date with the date in the dataframe:
today = datetime.today()
if df['Date'] > today is True:
print(df['Date])
But I get the error --> The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Upvotes: 2
Views: 7388
Reputation: 666
When you write the expression
if df['Date'] > today is True:
python doesn't know if you want to compare each of the elements of the serie to Today (which would return another serie of booleans) or the whole serie to today (which would return a single boolean, it would make no sense here).
In short if you want to extract all the columns with date later than today you should do something like:
df[df['Date'] > today]
which will print you the dataframe with only the Dates later than today.
Upvotes: 3