I am having a problem with formatting a time series in a pandas dataframe.
00:18:41.728.560.640
is my time object. It is hour:min:sec:ms:us:ns.
My approach was:
df['Time'] = pd.to_datetime(df['Time'], format="%H:%M:%S.%f")
Results in:
ValueError: unconverted data remains: .560.640
But this is only working for milliseconds. Is there another way to format the time or what am I doing wrong?
CodePudding user response:
Datetime struggles with the dots separating ms, us and ns. You can remove them with regex:
df['Time'] = pd.to_datetime(df['Time'].replace(r'(?<!:\d\d)\.', '', regex=True), format="%H:%M:%S.%f")
We use a negative lookbehind (?<!:\d\d)
to make sure the first dot isn't replaced by an empty string.
CodePudding user response:
Another option would be:
pd.to_timedelta(df['Time'].str[::-1].str.replace('.', '', 2).str[::-1])
0
0 0 days 00:18:41.728560640