Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2021-10-13

>Solution :

You can use

df['Date'] = pd.to_datetime(df['Date'], dayfirst=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading