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

Question:

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?

Asked By: Pablojim

||

Answers:

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:]) 
Answered By: waitingkuo

How about:

df.index.is_monotonic

Answered By: Wes McKinney

For non-indices:

df.equals(df.sort())
Answered By: nick_eu

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

Answered By: Manu Na Eira
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.