LLTeng
LLTeng

Reputation: 395

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

I have a panda column that's in <class 'pandas._libs.tslibs.timestamps.Timestamp'> and also a variable (date_1) that's in <class 'datetime.date'>

I like to calculate the month between both as follow but got the error TypeError: unsupported operand type(s) for -: 'DatetimeArray' and 'datetime.date'

df['Duration (months)']= (date_1 - df['Date'])/np.timedelta64(1,'M')

How can I calculate the duration in months between the variable of date_1 and df['Date']?

Upvotes: 0

Views: 12709

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

try with to_datetime():

date_1=pd.to_datetime(date_1)

OR

try with pd.Timestamp():

date_1=pd.Timestamp(date_1)

Finally:

df['Duration (months)']= (date_1 - df['Date'])/np.timedelta64(1,'M')

Upvotes: 3

Related Questions