Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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?

like image 694
Pablojim Avatar asked Jun 26 '13 09:06

Pablojim


People also ask

Is DataFrame sorted by index?

To sort a Pandas DataFrame by index, you can use DataFrame. sort_index() method. To specify whether the method has to sort the DataFrame in ascending or descending order of index, you can set the named boolean argument ascending to True or False respectively. When the index is sorted, respective rows are rearranged.

Is pandas DataFrame sorted?

Sort DataFrame in Pandas based on Multiple ColumnsThe DataFrame is first sorted by the column weight and then by height. Order Matters! See, how the results are different when you use different orders of the columns! Furthermore, you can also sort by multiple columns in different orders.

How do I sort pandas by index?

Sorts Pandas series by labels along the given axisThe sort_index() function is used to sort Series by index labels. Returns a new Series sorted by label if inplace argument is False, otherwise updates the original series and returns None. Axis to direct sorting. This can only be 0 for Series.


1 Answers

How about:

df.index.is_monotonic

like image 132
Wes McKinney Avatar answered Sep 21 '22 12:09

Wes McKinney