Skip to content
Advertisement

How to avoid Wrong recognition and Convention of Date – from String to Date format in Pandas Columns?

I have a Pandas column with dates as strings “12-10-2021 00:00” and “13-10-2021 00:00” i am using df['Date'] = df['Date'].astype('datetime64[ns]') output is coming as dates 2021-12-10 and 2021-10-13 where months and days are not converted correctly. enter image description here

How do i get the dates correctly as

2021-10-12

2021-10-13

Advertisement

Answer

You can use

df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement