Home > Blockchain >  convert timedelta to seconds
convert timedelta to seconds

Time:03-06

I calculated the duration in my dataframe, but I want to get the duration in seconds instead of what it gives (0 days, 00:10:25 and so on):

df['duration'] = df['end_date'] - df['start_date']
df.head(2)
    started_at  ended_at    start_date  end_date    duration
0   2021-01-23 16:14:19 2021-01-23 16:24:44 2021-01-23 16:14:19 2021-01-23 16:24:44 0 days 00:10:25
1   2021-01-27 18:43:08 2021-01-27 18:47:12 2021-01-27 18:43:08 2021-01-27 18:47:12 0 days 00:04:04

I tried some solutions from here, but I'm not really sure how to do it right, nothing works; for example I tried this:

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

but it says

TypeError: descriptor 'total_seconds' for 'datetime.timedelta' objects doesn't apply to a 'Series' object

CodePudding user response:

You should be able to use the total_seconds method. However, you'd need to access that method via the datetime accessor dt.

>>> df['duration'].dt.total_seconds()

However, if series Duration are strings/object- you should do use pd.to_timedelta

>>> pd.to_timedelta(df['duration']).dt.total_seconds()

CodePudding user response:

I would try running apply on your Series

df['duration'] = df['duration'].apply(datetime.timedelta.total_seconds)

From your error. It is stating that you are trying to run it on a Series

  • Related