Thi-Cav
Thi-Cav

Reputation: 31

How to convert time in days, hours, minutes, and seconds to only seconds

I have the following dataframe column:

Columm of Dataset

I need to convert object string data from the csv column into total seconds.

Example: 10m -> 600s


I tried this code:

df.duration = str(datetime.timedelta(df['duration']))

But the following error is displayed

TypeError: unsupported type for timedelta days component: Series

Upvotes: 3

Views: 3307

Answers (2)

Trenton McKinney
Trenton McKinney

Reputation: 62403

  • The correct, and vectorized way to convert 'duration' to seconds is to:
    1. Convert 'duration' to a timedelta
    2. Divide by pd.Timedelta(seconds=1)
  • The correct way to get seconds for only the hours, minutes and seconds component is to use .dt.seconds
  • See this answer for a thorough discussion of timedelta and why the .total_seconds method is a total accident.
import pandas as pd

# test data
df = pd.DataFrame({'duration': ['10d 15h 23m', '10d 18h 13m']})

# convert duration to a timedelta
df.duration = pd.to_timedelta(df.duration)

# calculate total_seconds
df['total_sec'] =  df.duration / pd.Timedelta(seconds=1)

# get seconds for just hours, minutes, seconds
df['sec_without_days'] = df.duration.dt.seconds

# display(df)
          duration  total_sec  sec_without_days
0 10 days 15:23:00   919380.0             55380
1 10 days 18:13:00   929580.0             65580

Upvotes: 3

hoomant
hoomant

Reputation: 455

You can use the pandas.to_timedelta() to convert your strings to timedelta objects and then apply a lambda function to get the total seconds in the duration:

import pandas as pd
df['duration'] = pd.to_timedelta(df['duration']).apply(lambda x: x.total_seconds())

Upvotes: 2

Related Questions