简体   繁体   中英

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

I have the following dataframe column:

数据集列

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

  • 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM