'pd.to_datetime' not giving correct output

Hello, I am using 'pd.to_datetime' on the below data (tradebook_4):



   trade_date  quantity

0   04/09/18       283

1   10/09/18        25

2   12/09/18       500

3   14/09/18        40

4   17/09/18       252

5   18/09/18      1100

6   19/09/18         1



and the output that I'm getting is:



                     quantity

trade_date          

2018-04-09       283

2018-10-09        25

2018-12-09       500

2018-09-14        40

2018-09-17       252

2018-09-18      1100

2018-09-19         1



Can anyone help why are there 2 diff date formats in the output (highlighted above in 2 diff colors)?



Below is the code that I'm using:



>>> tradebook_4.set_index('trade_date', inplace=True)

>>> tradebook_4.index = pd.to_datetime(tradebook_4.index)

>>> tradebook_4



Thanks,

Kanika

I think you forgot to add the format of the input date string column
Using the following code line surely should fix the issue:-

pd.to_datetime(tradebook_4.index, format='%d/%m/%Y')

Thanks for the response Aman! But on using this code, I’m getting a ‘Syntax error’. Its not identifying ‘%’.

My bad. I forgot to put %d/%m/%Y in quotes. It worked now. Thanks again Aman!

your welcome :slight_smile: