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:

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'))

Leave a Reply