Skip to content

PERF: Checking monotonic-ness before sorting on an index #11080 #11294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions asv_bench/benchmarks/frame_methods.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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