Reputation: 119
I want to create a column that identifies if the dates are less than 30 days from now (with boolean values). Here is the sample:
data={'Process_ID':['12345-98', '23547-75', '85763-99','44231-56','78456-00','53218-87'],
'Date': ['2021-06-30','2022-08-10','2021-06-15','2023-10-02','2024-04-03','2021-06-25']}
df=pd.DataFrame(data)
df['Date']=pd.to_datetime(df['Date'])
print(df)
The dataframe is the following:
Process_ID Date
0 12345-98 2021-06-30
1 23547-75 2022-08-10
2 85763-99 2021-06-15
3 44231-56 2023-10-02
4 78456-00 2024-04-03
5 53218-87 2021-06-25
Considering this table, how can I check if these dates are less than 30 days from now? Please, take into account that past dates like 15-06-2021 (dd/mm/yyyy) should not be registered as True. I just want future dates.
Upvotes: 0
Views: 2504
Reputation: 153460
Update per comment:
df.assign(dif = (df['Date'] - pd.Timestamp("now")).dt.days).eval('0 < dif < 30')
Output:
0 True
1 False
2 False
3 False
4 False
5 True
dtype: bool
Try:
df['Date'] = pd.to_datetime(df['Date'])
(df['Date'] - pd.Timestamp('now')).lt(pd.Timedelta(days=30))
Output:
0 True
1 False
2 True
3 False
4 False
5 True
Name: Date, dtype: bool
Upvotes: 1
Reputation: 261
df['new'] = df['Date'].apply(lambda x: True if
(x - pd.Timestamp.now() < pd.Timedelta('30 days'))
and x > pd.Timestamp.now() else False)
Upvotes: 2