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

Why different answers for the same logic in datetime conversion?

The output of converted date is ‘20210514’

input_date = '14-05-2021' # 14th May 2021
converted_date = (pd.to_datetime(input_date).strftime('%Y%m%d'))

But if I use the same logic below, why is it coming up like ‘20220601’ (1st June 2022)? The output I expect is ‘20220106’.

input_date = '06-01-2022' # 6th Jan 2022
converted_date = (pd.to_datetime(input_date).strftime('%Y%m%d'))

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

>Solution :

You haven’t provided a format for the input date string so pandas has to guess at it. 06-01-2022 is ambiguous: it could be month-day-year or day-month-year.

pandas.to_datetime() has a dayfirst argument that defaults to false. Since 06 is a valid month, it is being parsed as a month. 14 is not a valid month, so pandas figures out it should be the day.

Try this instead:

input_date = '06-01-2022' # 6th Jan 2022
converted_date = (pd.to_datetime(input_date, dayfirst=True).strftime('%Y%m%d'))

You can also pass in a format if you want to be really specific.

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