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?
>Solution :
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.