user3447653
user3447653

Reputation: 4158

Sort a pandas dataframe based on DateTime field

I am trying to sort a dataframe based on DateTime field which is of datatype datetime64[ns].

My dataframe looks like this:

Name    DateTime1
P38     NaT
P62     2016-07-13 16:03:32.771
P59     2016-06-23 14:23:42.461
P07     NaT
P16     2016-06-23 14:02:06.237
P06     2016-07-13 16:03:52.570
P106    2016-07-13 19:56:22.676

When I sort it using DateTime field,

df.sort_values(by='DateTime1',ascending=True)

I do not get the desired result.

Output:

Name    DateTime1
P16     2016-06-23 14:02:06.237
P59     2016-06-23 14:23:42.461
P62     2016-07-13 16:03:32.771
P06     2016-07-13 16:03:52.570
P106    2016-07-13 19:56:22.676
P38     NaT
P07     NaT

Upvotes: 20

Views: 60463

Answers (4)

user7864386
user7864386

Reputation:

I know this is an old question but OP seems to have wanted to put NaN values at the beginning since the output they posted is already sorted. In that case, there's a parameter in sort_values that controls where to put NaN values: na_position

df = df.sort_values(by='DateTime1', ascending=True, na_position='first')

Output:

   Name               DateTime1
0   P38                     NaT
3   P07                     NaT
4   P16 2016-06-23 14:02:06.237
2   P59 2016-06-23 14:23:42.461
1   P62 2016-07-13 16:03:32.771
5   P06 2016-07-13 16:03:52.570
6  P106 2016-07-13 19:56:22.676

Upvotes: 1

Jacob
Jacob

Reputation: 55

For others who might find this useful. I got it working by using the inplace argument like so:

df.sort_values(by='DateTime1', ascending=True, inplace=True)

Upvotes: 2

Adi Putra
Adi Putra

Reputation: 35

I try both codes below but is not working

df = df.sort_values(by='DateTime1', ascending=True)

or

df.set_index('DateTime1', drop=True, append=False, inplace=True, verify_integrity=False)

df = df.sort_index()

What I found working is convert the datetime column to an index column and afterward sort by index. So in your case is:

df=df.set_index('DateTime1')       
                                            
df=df.sort_index(ascending=False)

Upvotes: 1

ctrl-alt-delete
ctrl-alt-delete

Reputation: 3862

  1. Try assigning back to df, otherwise use inplace=True, but don't do both. See pandas.DataFrame.sort_values
df = df.sort_values(by='DateTime1', ascending=True)
  1. Otherwise, try pandas.DataFrame.set_index and then pandas.DataFrame.sort_index
df.set_index('DateTime1', drop=True, append=False, inplace=True, verify_integrity=False)
df = df.sort_index()

Upvotes: 37

Related Questions