Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert timedelta to floating-point

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.

like image 484
fidelitas Avatar asked Jan 28 '14 19:01

fidelitas


People also ask

How do you convert Timedelta to float?

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).

How do I convert Timedelta to int in Python?

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.


1 Answers

You could use the total_seconds method:

time_d_float = time_d.total_seconds() 
like image 58
unutbu Avatar answered Sep 23 '22 18:09

unutbu