0

I have a DataFrame that has time that has a 'Time Taken' column. The format is not a usual timestamp so I am struggling. The below is an extract of the column.

1 min 50 secs
2 mins 10 secs
40 secs 

How can I convert this column to produce an output in seconds? The below is what I am looking for.

110
130
40

Thanks so much.

student214
  • 95
  • 9
  • what is the exact format? Is it strings as shown? Can you show sample of the dataframe? – Justin Oberle Apr 26 '21 at 15:01
  • Does this answer your question? [How can I parse free-text time intervals in Python, ranging from years to seconds?](https://stackoverflow.com/questions/9775743/how-can-i-parse-free-text-time-intervals-in-python-ranging-from-years-to-second) – FObersteiner Apr 26 '21 at 15:05
  • see also [How to construct a timedelta object from a simple string](https://stackoverflow.com/q/4628122/10197418) – FObersteiner Apr 26 '21 at 15:06
  • @JustinOberle yes, the format is just strings as shown – student214 Apr 26 '21 at 15:10

2 Answers2

1

You can use the datetime library to parse the strings. I would recommend standardizing the dataframe strings so that you can use a single parser for the times.

from datetime import datetime

time = "1 min 50 secs"

dt = datetime.strptime(time,'%M min %S secs')

secs = dt.minute*60 + dt.second
print(secs)

110
Kyle K.
  • 21
  • 4
  • 1
    @MrFuppes That is why I mentioned standardizing the strings. If the OP changes the 40 secs to 0 min 40 secs it will work. – Kyle K. Apr 26 '21 at 15:15
1

You can use pd.to_timedelta function of pandas.

Input

df = pd.DataFrame({'time': ["1 min 50 secs", "2 mins 10 secs", "40 secs"]})


    time
0   1 min 50 secs
1   2 mins 10 secs
2   40 secs

Solution using pd.to_timedelta

df['timedelta'] = pd.to_timedelta(df.time.str.replace('secs','sec')
                .str.replace('mins', 'min')).dt.total_seconds().astype(int)

Output

    time           timedelta
0   1 min 50 secs   110
1   2 mins 10 secs  130
2   40 secs         40
Utsav
  • 5,572
  • 2
  • 29
  • 43