Deidraak
Deidraak

Reputation: 123

Pandas: date calculation and check is date exceeds date today

I have a dataframe with a date column. I need to add 6 months to the date column and check if that date exceeds the date today. I only want to see the rows that exceed the date today. The column is a datetime type.

dataframe:

         date

row1     2022-04-08 10:17:00
row2     2022-09-11 11:37:00                        
row3     2022-07-10 10:34:00                             
row4     2022-07-10 10:44:00                            
row5     2022-08-11 10:11:00

I tried:

I can add 181 days to the date column via pd.timedelta but I don't know show or filter based on the date today.

expected result:

Example with todays date 2022-12-06

         date                  date+6m

row1     2022-07-10 10:34:00   2023-01-07 10:34:00  
row2     2022-08-10 10:44:00   2023-02-07 10:44:00

Another option would be to show a column with a true and false but I prefer to see the actual date. Maybe that can be an extra column.

Thanks for the help!

Upvotes: 1

Views: 80

Answers (1)

J. M. Arnold
J. M. Arnold

Reputation: 6799

To add six months to your date, use offset: df["date_plus_6m"] = df["date"] + pd.DateOffset(months=6)

If you want to filter the rows based on today's date, you can use datetime.date.today(): df = df[df["date_plus_6m"] > current_date]!

Upvotes: 1

Related Questions