0

I am using pd.to_datetime to convert the date column, but it's not properly converting for the cells that have 10/1/2022, but all the other cells are correct. Yes, trust me. I know it's weird. The raw date format is date/month/year

Dataframe = df

Customer ID    Date
ABC123       10/1/2022
ABC223       30/12/2021
ABC456       21/6/2022
ABC876       30/3/2022

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

Results

Customer ID    Date
 ABC123     2022-10-01
 ABC223     2021-12-30
 ABC456     2022-06-21
 ABC876     2022-03-30

If you see for the customer #ABC123, the date is incorrectly showing as 2022-10-01 when it's supposed to be 2022-01-10. But for all the other cells, it correctly shows yyyy-mm-dd except for customer #ABC123

Ziggy
  • 113
  • 6
  • I see the input being wrong. The raw date format mentioned as date/month/year but the value in 2nd row is 12/30/2021 implies month is 30 which is weird. – Asetti sri harsha Aug 25 '22 at 15:51
  • Sorry, typing mistake. I edited my posted. – Ziggy Aug 25 '22 at 15:57
  • 1
    `pd.to_datetime` takes a format arg `pd.to_datetime(df['Date'],format='%d/%m/%Y')` will close this it's surely a dupe – Umar.H Aug 25 '22 at 15:57
  • [convert-pandas-column-to-datetime](https://stackoverflow.com/questions/26763344/convert-pandas-column-to-datetime) – Umar.H Aug 25 '22 at 15:59
  • Thanks, it worked. But why did just that cell have the issue and not any other cells? – Ziggy Aug 25 '22 at 16:03
  • 1
    Because the formatter will first use the default format then attempt to work out a format, open an IDE, change the settings so you can dive into the code base when you debug, add a break point and debug the code to see what's happening – Umar.H Aug 25 '22 at 19:12

0 Answers0