Convert timedelta to floating-point Convert timedelta to floating-point python python

Convert timedelta to floating-point


You could use the total_seconds method:

time_d_float = time_d.total_seconds()


In Python 3.2 or higher, you can divide two timedeltas to give a float. This is useful if you need the value to be in units other than seconds.

time_d_min = time_d / datetime.timedelta(minutes=1)time_d_ms  = time_d / datetime.timedelta(milliseconds=1)


You could use numpy to solve that:

import pandas as pdimport numpy as nptime_d = datetime_1 - datetime_2#for a single valuenumber_of_days =pd.DataFrame([time_d]).apply(np.float32)#for a Dataframenumber_of_days = time_d.apply(np.float32)

Hope it is helpful!