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

Question:

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

Asked By: mrsprawin

||

Answers:

You can use

df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)
Answered By: Ynjxsjmh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.