Skip to content

Commit

Permalink
PERF: Checking monotonic-ness before sorting on an index #11080
Browse files Browse the repository at this point in the history
  • Loading branch information
scari committed Oct 14, 2015
1 parent 51a70dc commit b40c1ab
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions asv_bench/benchmarks/frame_methods.py
Expand Up @@ -930,6 +930,16 @@ def time_frame_xs_row(self):
self.df.xs(50000)


class frame_sort_index(object):
goal_time = 0.2

def setup(self):
self.df = DataFrame(randn(1000000, 2), columns=list('AB'))

def time_frame_sort_index(self):
self.df.sort_index()


class series_string_vector_slice(object):
goal_time = 0.2

Expand Down
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.17.1.txt
Expand Up @@ -52,6 +52,8 @@ Deprecations
Performance Improvements
~~~~~~~~~~~~~~~~~~~~~~~~

- Checking monotonic-ness before sorting on an index (:issue:`11080`)

.. _whatsnew_0171.bug_fixes:

Bug Fixes
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/frame.py
Expand Up @@ -3157,6 +3157,15 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
else:
from pandas.core.groupby import _nargsort

# GH11080 - Check monotonic-ness before sort an index
# if monotonic (already sorted), return None or copy() according to 'inplace'
if (ascending and labels.is_monotonic_increasing) or \
(not ascending and labels.is_monotonic_decreasing):
if inplace:
return
else:
return self.copy()

indexer = _nargsort(labels, kind=kind, ascending=ascending,
na_position=na_position)

Expand Down

0 comments on commit b40c1ab

Please sign in to comment.