Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas Format time Nanoseconds

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading