0

I have a DataFrame with column contains string with time format. It's looks like this 1'29.30" and it represent 1 minute 29 seconds and 30 centisecond (30/1000 seconds).

I would like to convert it to time format. My only idea is to create regexp expresion for it.

There is an other way for this task?

Eg. of my df['time_str']

124    1'29.30"
125    1'29.36"
126    1'29.54"
127    1'29.93"
128    1'30.62"
129    1'30.80"
130    1'30.83"
131    1'31.22"
Name: time_str, dtype: object
Corralien
  • 109,409
  • 8
  • 28
  • 52
CezarySzulc
  • 1,849
  • 1
  • 14
  • 30
  • 1
    Related, possible duplicate: [How to construct a timedelta object from a simple string](https://stackoverflow.com/q/4628122/11082165) – Brian61354270 Aug 27 '21 at 20:01

2 Answers2

1

You can use pandas.to_datetime with a custom format '%M\'%S.%f"', then keep only the time part (without the date part)

import pandas as pd

data = ['1\'29.30"', '1\'29.36"', '1\'29.54"', '1\'29.93"',
        '1\'30.62"', '1\'30.80"', '1\'30.83"']

df = pd.DataFrame(data, columns=['time'])
df['time'] = pd.to_datetime(df['time'], format='%M\'%S.%f"').dt.time
Brian61354270
  • 8,690
  • 4
  • 21
  • 43
azro
  • 53,056
  • 7
  • 34
  • 70
0

I think regex would be the best bet, but rather than a time format you are likely looking for a timedelta. Time would be a given time of day as an example, whereas you appear to have a measurement of time passing. Try using the following, I tested with an individual time but I'll leave it to you to plug this into the dataframe to convert each string.

import re
from datetime import timedelta

original = "1'29.30\""
regex = re.compile("([0-9]*)'([0-9]*).([0-9]*)\"")
matches = regex.match(original).groups()
# Notice that I had to convert centiseconds to milliseconds
delta = timedelta(minutes=int(matches[0]), seconds=int(matches[1]), milliseconds=int(matches[2])*10)