0

I have a dataframe created using an Access table. The Date column is formatted as an object and has the following syntax:

2019-12-31 12:55:00

I want to sort the dataframe on ascending date order so converted the column to datetime using:

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

I then try to sort using the following but it doesn't work. The df looks exactly as it did before:

df.sort_values(by='Date')

An extract of the df is below. What am I doing wrong?

Date RefTT
31/12/2019 12:55 10107355
31/12/2020 15:25 36643074
31/12/2020 14:15 36868924
08/01/2019 17:35 24763287
08/01/2019 19:45 10073929
08/01/2019 14:50 24132711
08/01/2019 18:40 24576111
08/01/2019 14:40 12255761
08/01/2019 13:30 18443380
31/12/2020 14:00 25106015
31/01/2019 15:10 19629874
31/01/2019 19:30 14505238
31/01/2020 15:40 11930839
31/01/2020 13:40 8753735
31/01/2020 14:45 23523591
31/01/2020 17:15 27388541
31/01/2021 15:10 28575475
28/02/2019 16:30 21470086
28/02/2019 16:55 18136828
28/02/2019 13:35 11896776
28/02/2019 13:50 14708670
28/02/2019 14:25 16095243
29/02/2020 15:00 26641007
29/02/2020 15:45 24342002
29/02/2020 13:37 1532111
29/02/2020 16:10 26684160
28/02/2021 16:40 6320524
28/02/2021 16:20 27632002
28/02/2021 15:10 21104540
31/03/2019 17:40 6896639
31/03/2021 15:10 38036656
31/03/2021 17:20 21281524
31/03/2021 15:00 24986895
31/03/2021 15:50 26600969
3
  • 1
    df = df.sort_values(by='Date') ? Do you assig back? Commented Jan 26, 2023 at 8:22
  • 1
    pd.to_datetime is tricky. 5/6 times I mess up the actual data. Try to put the format in: format='%Y%m%d' Commented Jan 26, 2023 at 8:23
  • 1
    df['Date'] = pd.to_datetime(df['Date'], dayfirst=True) working? Because if use df['Date'] = pd.to_datetime(df['Date']) ouput should be different - YYYY-MM-DD HH:MM Commented Jan 26, 2023 at 8:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.