简体   繁体   中英

Date structure changing in python when I extract data from csv

I am pulling data from CSV and the Date column is in the format "dd/mm/YYYY" but looks like when I convert dates from object to datetime format some of the dates are changing weirdly.

I have showed the example below, the last there entries are for 7th March, 6th March and 6th March. After converting to datetime those dates changed to 3rd July, 3rd June and 3rd June.

How do I fix the issue?

在此处输入图像描述

Maybe you can try a pandas.to_datetime argument "dayfirst"

So the code will look like:

    pd.to_datetime(df.index, dayfirst=True) 

And it should fix it. In documentation a information about this argument looks:

"Specify a date parse order if arg is str or its list-likes. If True, parses dates with the day first, eg 10/11/12 is parsed as 2012-11-10"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM