TransWikia.com

How to turn a series that contains pandas.datetime.timedelta to floats e.g. hours or days?

Data Science Asked on July 21, 2021

I want to plot some data that contains timedeltas. However, the numpy plot() and errorbar() return:

TypeError: float() argument must be a string or a number, not 'Timedelta'

The question is how can I efficiently convert the timedeltas to a float or integer number. E.g. when I use minutes as base:

0 days 01:30:00 -> 90

or hours

0 days 01:30:00 -> 1.5

Similar to Series.dt.days which would return 0 in this case.

2 Answers

One way to do this would be to use Series.dt.seconds and ´Series.dt.days´ and multiply with a factor for the desired unit:

(Series.dt.seconds/3600) + (Series.dt.days*24)  # for values with [hours]

Answered by Sören on July 21, 2021

You can divide by the timedelta you want to use as unit:

totalDays = my_timedelta = pd.TimeDelta('1D')

Answered by pqnet on July 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP