Python Covert Duration String to Number Format in Hours

63 Views Asked by At

I have a Pandas dataframe with a Duration column with contains durations as text with the following format. Some strings have "Days" added at the beginning where some of them just have the hour minute and second information:

df = 
    Duration
0   16h:48m:31s
1   0h:02m:49s
2   1d 3h:57m:27s
...

I want to convert this into a numeric format in the units of Hours. How would you approach this problem? Thanks in advance.

2

There are 2 best solutions below

0
jezrael On

Use to_timedelta with Series.dt.total_seconds:

df['Hours'] = pd.to_timedelta(df['Duration']).dt.total_seconds().div(3600)

print (df)
        Duration      Hours
0    16h:48m:31s  16.808611
1     0h:02m:49s   0.046944
2  1d 3h:57m:27s  27.957500
0
pensive On
import pandas as pd

# Sample DataFrame
data = {'Duration': ['16h:48m:31s', '0h:02m:49s', '1d 3h:57m:27s']}
df = pd.DataFrame(data)

def parse_duration(duration_str):
    parts = duration_str.split()
    total_hours = 0

    for part in parts:
        if 'd' in part:
            days = int(part.replace('d', ''))
            total_hours += days * 24  # Convert days to hours
        else:
            time_parts = part.split(':')
            hours, minutes, seconds = map(int, time_parts)
            total_hours += hours + minutes / 60 + seconds / 3600

    return total_hours

df['Duration_in_hours'] = df['Duration'].apply(parse_duration)

print(df)