0

In my CSV I have a column with time durations written as 13:08.4 and 13:06.20.

I would like to convert this column into a pandas timedelta.

However when I try df['Time'] = pd.to_timedelta(df['Time']) I get an error ValueError: expected hh:mm:ss format before .

What is going on and how do I fix it?

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
Jsevillamol
  • 2,425
  • 2
  • 23
  • 46
  • 1
    This answer seems to be very close to what you want: [Convert string to timedelta in pandas](https://stackoverflow.com/a/53543307/1609514). Try just changing the `.astype(int)` to `.astype(float)`. – Bill Aug 19 '21 at 13:10

1 Answers1

1

You can replace '.' by ':' like error said to make it in format of hh:mm:ss:

df['Time'] = pd.to_timedelta(df['Time'].str.replace('.',':'))
#13:08.4 becomes: 0 days 13:08:04

OR

If the format of 'Time' is mm:ss then use:

df['Time'] = pd.to_timedelta('00:'+df['Time'])
#13:08.4 becomes: 0 days 00:13:08.400000
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41