Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to solve incorrect dates after conversion from objects?

I have a dataframe which looks like this:

Date         ID   Product
01/10/2018   01   XM0
01/10/2018   02   XM0
02/10/2018   02   BY2

The date is currently being recognised as an object so I run the following code to convert to datetime:

df['Date'] = pd.to_datetime(df['Date'])

For some reason when I run this line of code the output shows that the month of the date has been changed to January when it should be October:

Date         ID   Product
2018-01-10   01   XM0
2018-01-10   02   XM0
2018-02-10   02   BY2

Is there a way to prevent this so that the dates are accurately converted into the actual date when I run the datetime conversion line?

Thank you :)

like image 259
R Sem Avatar asked Dec 09 '25 05:12

R Sem


1 Answers

Use dayfirst=True

Ex:

df["Date"] = pd.to_datetime(df["Date"], dayfirst=True)
like image 76
Rakesh Avatar answered Dec 10 '25 17:12

Rakesh