0

I have a df where I'd like to filter the table by a single date column, specifically dates that are within 1 year of today.

I've tried the following with no luck:

df[df['latest start date'] > (date.today() + timedelta(weeks=-52))]

The error message I get is:

TypeError: '>' not supported between instances of 'str' and 'datetime.date'

Any help would be greatly appreciated.

1
  • 3
    Transform your date to datetime: df['latest start date'] = pd.to_datetime(df['latest start date'] ) before that line. Commented Apr 23, 2020 at 13:06

1 Answer 1

2

Your date column is not of type date.

df['latest start date'] = pd.to_datetime(df['latest start date'])

Now you can compare the two because they are both of type datetime

Sign up to request clarification or add additional context in comments.

3 Comments

One step closer, thank you. However I now get this TypeError: Invalid comparison between dtype=datetime64[ns] and date
Actually, I solved it. Just amended the formula to this df[df['latest start date'] > (datetime.datetime.now() + timedelta(weeks=-52))] after your suggestion.
Glad I could help, yep datetime and date are different I should have caught that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.