78

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
fidelitas
  • 1,113
  • 2
  • 12
  • 14

6 Answers6

105

You could use the total_seconds method:

time_d_float = time_d.total_seconds()
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
47

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)
dan04
  • 87,747
  • 23
  • 163
  • 198
  • 1
    I see this as the most natural way to do it. Besides that, it's also great for adapting time implementations of other languages/systems. – Wolf Sep 17 '19 at 09:02
6

You could use numpy to solve that:

import pandas as pd
import numpy as np

time_d = datetime_1 - datetime_2

#for a single value
number_of_days =pd.DataFrame([time_d]).apply(np.float32)

#for a Dataframe
number_of_days = time_d.apply(np.float32)

Hope it is helpful!

  • 1
    I get this error with your suggestion: AttributeError: 'datetime.timedelta' object has no attribute 'apply' – lancew Sep 21 '17 at 20:06
  • Hi @lancew, thanks for catching that! In that case, I needed to convert a dataframe to float so it worked out for me. If you want to convert a unique value, you could do it: `pd.DataFrame([time_d]).apply(np.float32)` . In this case, you need to import both 'pandas as pd' and 'numpy as np'. Although I'll be testing other ways. Please, let me know if it works. – Monique Marins Sep 22 '17 at 14:59
  • 2
    Now, I get this error: "TypeError: float() argument must be a string or a number, not 'Timedelta'" – supercooler8 May 04 '21 at 10:20
  • I had a similar issue. I solved it using `to_numpy()` e.g. `df["column"].to_numpy().astype(float)`. Also see solution here https://stackoverflow.com/questions/49370756/python-pandas-dataframe-how-to-convert-timedelta-column-to-float-column. – Just_Alex Feb 14 '22 at 03:08
1

If you needed the number of days as a floating number you can use timedelta's days attribute

time_d = datetime_1 - datetime_2
number_of_days = float(time_d.days)
RoachLord
  • 993
  • 1
  • 14
  • 28
  • 4
    This gives the number of days only. You lose whatever amount of hours minutes or seconds you have in time_d. This is not accurate. – lpnorm Jun 28 '21 at 11:37
0
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).
-5

I had the same problem before and I used timedelta.total_seconds to get Estimated duration into seconds with float and it works. I hope this works for you.

from datetime import timedelta,datetime

time_d = datetime_1 - datetime_2

time_d.total_seconds()                  
Ironcache
  • 1,719
  • 21
  • 33
Faith
  • 1
  • 1