Convert a countdown like date format to seconds in Python

Submitted 4 years, 7 months ago
Ticket #115
Views 359
Language/Framework Python
Priority Low
Status Closed

How to to convert object string data from the csv column into total seconds.

In my csv sheet, data looks like below,

Duration

1hr 5m

2 hr 10m

Example: 1m -> 60s ( I would like to convert like this). But I am getting below error,

TypeError: unsupported type for timedelta days component: Series

Submitted on Sep 06, 20
add a comment

1 Answer

Verified

I think we 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())

Submitted 4 years, 7 months ago


Latest Blogs