'convert timedelta to seconds

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


Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 daemon