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 datetime format to include 'T'

I have a dataframe with multiple datetime columns with this format:

2019-02-19 09:47:45.285

the required format is:

2019-02-19T09:47:45.285

Tried with the following code, but I get the same thing:

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[columns] = df[columns].apply(pd.to_datetime, format='%Y-%m-%dT%H:%M:%S.%f')

>Solution :

The format parameter tells pd.to_datetime how to parse the date. Since it supports the format of your dates automatically, I’d remove that.

What you really want is to format your date back to a string, and you can use column.dt.strftime with your format for that:

df[columns] = df[columns].apply(pd.to_datetime).apply(lambda col: col.dt.strftime('%Y-%m-%dT%H:%M:%S.%f'))
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