Lucas Dresl
Lucas Dresl

Reputation: 1170

datetime is chaning date and month wrongly for some cases

I am trying to change a object to a datetime value.

 id        date
  1  07/03/2020
  2  20/02/2020

In the above df the column date is in the format %d%m%Y in strings value and when I apply df['date'] = pd.datetime(df['date']) i have the following results:

 id        date
  1  2020-07-03
  2  2020-02-20

and the format of this new datetime column is on %Y%m%d. It is very strange that the first value df has change the day for the month and the month for the day meanwhile in the second row has converted correctly mantaining the values for day and month.

Upvotes: 0

Views: 52

Answers (1)

ragad
ragad

Reputation: 27

You should try specifying the format when you convert:

import pandas as pd

df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y')

Upvotes: 2

Related Questions