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

How to convert duration strings to seconds?

I have such a column in a pandas dataframe:

duration
1 day 22:12:15.778543
2 days 10:09:07.118723
00:18:23.985112

I would like to convert this duration to seconds.

How can I do this? I am not sure if this is possible because of the special string format I got (1 day, 2 days etc.)?

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

>Solution :

Use to_timedelta with Series.dt.total_seconds:

df['s'] = pd.to_timedelta(df['duration']).dt.total_seconds()
print (df)
                 duration              s
0   1 day 22:12:15.778543  166335.778543
1  2 days 10:09:07.118723  209347.118723
2         00:18:23.985112    1103.985112
    
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