简体   繁体   中英

How to filter dates within a range in Pandas DataFrame?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM