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

Question:

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.

  ID   Date       Employed? 
   3   3/02/2023      Y

Any suggestions?

Asked By: Coding_Nubie

||

Answers:

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
Answered By: It_is_Chris