'Pandas read format %D:%H:%M:%S with python

Currently I am reading in a data frame with the timestamp from film 00(days):00(hours clocks over at 24 to day):00(min):00(sec)

pandas reads time formats HH:MM:SS and YYYY:MM:DD HH:MM:SS fine. Though is there a way of having pandas read the duration of time such as the DD:HH:MM:SS.

Alternatively using timedelta how would I go about getting the DD into HH in the data frame so that pandas can make it "1 day HH:MM:SS" for example

Data sample

00:00:00:00
00:07:33:57 
02:07:02:13 
00:00:13:11 
00:00:10:11 
00:00:00:00 
00:06:20:06 
01:12:13:25 

Expected output for last sample

36:13:25

Thanks



Solution 1:[1]

Convert days separately, add to times and last call custom function:

def f(x):
    ts = x.total_seconds()
    hours, remainder = divmod(ts, 3600)
    minutes, seconds = divmod(remainder, 60)
    return ('{}:{:02d}:{:02d}').format(int(hours), int(minutes), int(seconds)) 


d = pd.to_timedelta(df['col'].str[:2].astype(int), unit='d')
td = pd.to_timedelta(df['col'].str[3:])
df['col'] =  d.add(td).apply(f)
print (df)
        col
0   0:00:00
1   7:33:57
2  55:02:13
3   0:13:11
4   0:10:11
5   0:00:00
6   6:20:06
7  36:13:25

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1