PyNoob
PyNoob

Reputation: 223

Pandas not sorting datetime columns?

I have a dataframe as:

df:
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|     |   Unnamed: 0 | country                 | league                       | game                                               | home_odds   |   draw_odds |   away_odds |   home_score | away_score   | datetime            |
+=====+==============+=========================+==============================+====================================================+=============+=============+=============+==============+==============+=====================+
|   0 |            0 | Chile                   | Primera Division             | Nublense - A. Italiano                             | 2.25        |        3.33 |        3.11 |            1 | 0            | 2021-06-08 00:30:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|   1 |            1 | China                   | Jia League                   | Zibo Cuju - Shaanxi Changan                        | 11.54       |        4.39 |        1.31 |          nan | nan          | 2021-06-08 08:00:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|   2 |            2 | Algeria                 | U21 League                   | Medea U21 - MC Alger U21                           | 2.38        |        3.23 |        2.59 |          nan | nan          | 2021-06-08 09:00:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|   3 |            3 | Algeria                 | U21 League                   | Skikda U21 - CR Belouizdad U21                     | 9.48        |        4.9  |        1.25 |          nan | nan          | 2021-06-08 09:00:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|   4 |            4 | China                   | Jia League                   | Zhejiang Professional - Xinjiang Tianshan          | 1.2         |        5.92 |       12.18 |          nan | nan          | 2021-06-08 10:00:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+

I have defined datetime as datetime

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

and then tried to sort it

df.sort_values(by=['datetime'], ascending=True)

However the sorting does not work.

Can anybody help me understand why?

Please find the entire dataframe here for reference.

p.s. I am unable to paste the entire dataframe here because of character constraints.

Upvotes: 0

Views: 52

Answers (1)

Ben Y
Ben Y

Reputation: 1023

I see in the comments you already found your solution. Copying the df back into itself after calling sort_values() means it's "new" name is the old name.

I'll add this as an answer.

df.sort_values(by=['datetime'], ascending=True, inplace=True)

Then it should make the sorting in-place, so you don't have to assign it to itself.

Upvotes: 1

Related Questions