0

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 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

0
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)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.