Home > Blockchain >  Type Error with operations with dates (datetime package)
Type Error with operations with dates (datetime package)

Time:12-17

I have this code:

 import datetime

 last_date = datetime.datetime(2021, 1, 15)
 first_date = datetime.datetime(2021, 1, 1)

 date_1 = last_date - first_date

 print(date_1) #this prints: 14 days, 00:00:00

 r=0.05

 fa = 1/(1 r)**(date_1/360)

 fa

I get this error: TypeError: unsupported operand type(s) for ** or pow(): 'float' and 'datetime.timedelta'

I'm interested to the number of the days, not with the hours

CodePudding user response:

date_1 is a timedelta object, and as you've seen you can't divide it by a float. From the context, it looks like you're trying to get the number of days from it. You can extract it by using the days property:

fa = 1/(1 r)**(date_1.days/360)
# Here --------------^

CodePudding user response:

date_1 is a datetime.timedelta object. You need to get the number of days as an integer.

import datetime

 last_date = datetime.datetime(2021, 1, 15)
 first_date = datetime.datetime(2021, 1, 1)

 date_1 = last_date - first_date

 print(date_1) #this prints: 14 days, 00:00:00

 r=0.05

 fa = 1/(1 r)**(date_1.days/360)

 fa
  • Related