0

I am trying to identify records that expire within 1 year of today's date. This is the code I have and it doesn't work because I can't add or subtract integers from dates. Can someone assist? I know this is simple.

from datetime import date

today = date.today()

mask = (df['[PCW]Contract (Expiration Date)'] <= today + 365)
Alex Dowd
  • 39
  • 9

1 Answers1

1

You need to use time deltas.

from datetime import timedelta
one_year = timedelta(days=365)
mask = (df['[PCW]Contract (Expiration Date)'] <= today + one_year)

Assuming you are using datetime objects in your dataframe.

UPDATE

import pandas as pd
import numpy as np
df = pd.DataFrame({'[PCW]Contract (Expiration Date)' :["2020-01-21T02:37:21", '2021-01-21T02:37:21', '2022-01-21T02:37:21']})
s = pd.to_datetime(df['[PCW]Contract (Expiration Date)'])
one_year = np.timedelta64(365,'D')
today = np.datetime64('today')
mask = s <= today + one_year
mask

Output

0     True
1     True
2    False
Name: [PCW]Contract (Expiration Date), dtype: bool
Oddaspa
  • 731
  • 5
  • 21