0

I want to represent some data I have in the following format : 120 hours, 24 minutes, 45 seconds. I spent some time looking for the appropriate formating function to use in Python or Pandas, but could not find any corresponding properly to the one I need.

The data I have looks like this :

197hr 54min 0sec

200hr 28min 49sec

147hr 13min 58sec

datetime.time could work, but I'm limited in range [0, 23] for hours count.

I could go with my own formating style, but I would prefer to have a "standard format" that I can work with (calculating offsets, translate it in other formats, etc.).

1
  • How looks input data? Can you add some data sample column?
    – jezrael
    Sep 21, 2020 at 10:08

2 Answers 2

1

You want to use Timedelta objects. datetime has one too, but I prefer pandas

import pandas as pd
td = pd.Timedelta('200hr 28min 49sec')

You can add, subtract and divide timedeltas. To do more calculations you might want to work with td.total_seconds().

But printing the timedelta gives you a different format:

>>> td
Timedelta('8 days 08:28:49')

Solution: Write your own format function

def timedelta_to_str(td):
    h = td // pd.Timedelta('1h')
    td -= pd.Timedelta(hours = h)
    m = td // pd.Timedelta('1m')
    td -= pd.Timedelta(minutes = m)
    s = td.seconds
    return f'{h}hr {m}min {s}sec'
>>> timedelta_to_str(td)
200hr 28min 49sec

To use this format within a DataFrame you might want to use this hack:

pd.Timedelta.__repr__ = timedelta_to_str

This changes the default representation of all Timedelta Objects!

Or you might want to use inheritance to make it super clean:

class myTimedelta(pd.Timedelta):
    def __repr__(self):
        td = pd.Timedelta(self) # make a copy
        h = td // pd.Timedelta('1h')
        td -= pd.Timedelta(hours = h)
        m = td // pd.Timedelta('1m')
        td -= pd.Timedelta(minutes = m)
        s = td.seconds
        return f'{h}hr {m}min {s}sec'
>>> myTimedelta('200hr 28min 49sec')
200hr 28min 49sec
>>> myTimedelta('200hr 28min 49sec') + myTimedelta('1hr')
201hr 28min 49sec
1

You can create column filled by timedeltas:

df['td'] = pd.to_timedelta(df["data"])
print (df)
                data              td
0   197hr 54min 0sec 8 days 05:54:00
1  200hr 28min 49sec 8 days 08:28:49
2   231hr 7min 15sec 9 days 15:07:15
3  228hr 36min 13sec 9 days 12:36:13
4   221hr 36min 0sec 9 days 05:36:00
5    222hr 8min 6sec 9 days 06:08:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.