Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading