Pablojim
Pablojim

Reputation: 8582

How can I check if a Pandas dataframe's index is sorted

I have a vanilla pandas dataframe with an index. I need to check if the index is sorted. Preferably without sorting it again.

e.g. I can test an index to see if it is unique by index.is_unique() is there a similar way for testing sorted?

Upvotes: 53

Views: 25198

Answers (4)

Manu Na Eira
Manu Na Eira

Reputation: 91

Just for the sake of completeness, this would be the procedure to check whether the dataframe index is monotonic increasing and also unique, and, if not, make it to be:

if not (df.index.is_monotonic_increasing and df.index.is_unique):
  df.reset_index(inplace=True, drop=True)

NOTE df.index.is_monotonic_increasing is returning True even if there are repeated indices, so it has to be complemented with df.index.is_unique.

API References

Upvotes: 6

nick_eu
nick_eu

Reputation: 3723

For non-indices:

df.equals(df.sort())

Upvotes: 1

Wes McKinney
Wes McKinney

Reputation: 105611

How about:

df.index.is_monotonic

Upvotes: 95

waitingkuo
waitingkuo

Reputation: 93924

If sort is all allowed, try

all(df.sort_index().index == df.index)

If not, try

all(a <= b for a, b in zip(df.index, df.index[1:]))

The first one is more readable while the second one has smaller time complexity.

EDIT

Add another method I've just found. Similar with the second one but the comparison is vetorized

all(df.index[:-1] <= df.index[1:]) 

Upvotes: 2

Related Questions