Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter on dates n days from from today using Python

iI am fairly new to Python and have not used datetime/timedelta too much, I am trying to look at a dataset I have from the past 90 days and I have seen how to get todays date, but have no clue how to use date.today() into filter.

You can assume the date looks like this:

  ID     Date        Employed?
   1     12/11/2021     Y
   2     2/12/2022      Y
   3     3/02/2023      Y

And I would like it to filter to only see 90 days prior today.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  ID   Date       Employed? 
   3   3/02/2023      Y

Any suggestions?

>Solution :

import pandas as pd
import datetime

# get today's date and subtract 90 days
day_90 = datetime.datetime.today() - datetime.timedelta(days=90)

# make sure your date column is datetime format
df['Date'] = pd.to_datetime(df['Date'])

# boolean indexing to filter out dates less than 90 day from today
print(df[df['Date'] > day_90])

   ID       Date Employed?
2   3 2023-03-02         Y
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading