'Convert Dates pandas datetime

I'm trying to convert dates in the format '31-Aug-91' into '31-08-1991' using pandas datetime.

I've tried pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%m-%y').dt.date

But, I get the error ValueError: time data '31-Aug-91' does not match format '%d-%m-%y' (match)

How do I fix this?



Solution 1:[1]

Use %b for match first 3 letters of month names:

pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%b-%y').dt.date

If need format DD-MM-YYYY:

pd.to_datetime(df['INCIDENT_DATE'], format = '%d-%b-%y').dt.strftime('%d-%m-%Y')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jezrael