Convert multiple date formats to datetime in pandas

Viewed 971

I have a row of messy data where date formats are different and I want them to be coherent as datetime in pandas

df:
          Date
0    1/05/2015
1  15 Jul 2009
2     1-Feb-15
3   12/08/2019

When I run this part:

df['date'] = pd.to_datetime(df['date'], format='%d %b %Y', errors='coerce')

I get

        Date
0        NaT
1 2009-07-15
2        NaT
3        NaT

How do I convert it all to date time in pandas?

1 Answers

pd.to_datetime is capabale of handling multiple date formats in the same column. Specifying a format will hinder its ability to dynamically determine the format, so if there are multiple types do not specify the format:

import pandas as pd

df = pd.DataFrame({
    'Date': ['1/05/2015', '15 Jul 2009', '1-Feb-15', '12/08/2019']
})

df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
print(df)
        Date
0 2015-01-05
1 2009-07-15
2 2015-02-01
3 2019-12-08

*There are limitations to the ability to handle multiple date times. Mixed timezone aware and timezone unaware datetimes will not process correctly. Likewise mixed dayfirst and monthfirst notations will not always parse correctly.

Related