Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find number of days between today and future date?

I have a field named 'MATURITY' in a dataframe. One sample date looks like this:

2026-05-21

I'm trying to add a new field to the dataframe and find the difference between each maturity date and today. How can I do that? I tried the following:

df['DaysToMaturity'] = pd.to_datetime((df['MATURITY'] - date.today()).days)

That gives me this error:

TypeError: unsupported operand type(s) for -: 'DatetimeIndex' and 'datetime.date'

That should be pretty close, I believe, but obviously something is off here. Thoughts?

like image 783
ASH Avatar asked Nov 28 '25 18:11

ASH


1 Answers

Use pandas.Timestamp

df['DaysToMaturity'] = (df['MATURITY'] - pd.Timestamp('now')).dt.days
like image 182
piRSquared Avatar answered Dec 01 '25 07:12

piRSquared