I got a timedelta object from the subtraction of two datetimes. I need this value as floating point for further calculations. All that I've found enables the calculation with floating-points, but the result is still a timedelta object.
time_d = datetime_1 - datetime_2 time_d_float = float(time_d)
does not work.
from datetime import timedelta,datetime x1= timedelta(seconds=40, minutes=40, hours=5) x2= timedelta( seconds=50, minutes=20, hours=4) x3=x1-x2 x5 = x3. total_seconds() print(x5) print(type(x5)) print(type(x1)) print(x1) # if you are working with Dataframe then use loop (* for-loop).
Convert the timedelta to int Using the dt Attribute in Pandas. To convert the timedelta to an integer value, we can use the pandas library's dt attribute. The dt attribute allows us to extract components of the timedelta . For example, we can extract the year, month, day, minutes, or seconds using the dt attribute.
You could use the total_seconds
method:
time_d_float = time_d.total_seconds()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With