Skip to content

Commit 9bcc1a8

Browse files
committed
Merge pull request #11294 from scari/master
PERF: Checking monotonic-ness before sorting on an index #11080
2 parents 51a70dc + b40c1ab commit 9bcc1a8

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

asv_bench/benchmarks/frame_methods.py

+10
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,16 @@ def time_frame_xs_row(self):
930930
self.df.xs(50000)
931931

932932

933+
class frame_sort_index(object):
934+
goal_time = 0.2
935+
936+
def setup(self):
937+
self.df = DataFrame(randn(1000000, 2), columns=list('AB'))
938+
939+
def time_frame_sort_index(self):
940+
self.df.sort_index()
941+
942+
933943
class series_string_vector_slice(object):
934944
goal_time = 0.2
935945

doc/source/whatsnew/v0.17.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Deprecations
5252
Performance Improvements
5353
~~~~~~~~~~~~~~~~~~~~~~~~
5454

55+
- Checking monotonic-ness before sorting on an index (:issue:`11080`)
56+
5557
.. _whatsnew_0171.bug_fixes:
5658

5759
Bug Fixes

pandas/core/frame.py

+9
Original file line numberDiff line numberDiff line change
@@ -3157,6 +3157,15 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
31573157
else:
31583158
from pandas.core.groupby import _nargsort
31593159

3160+
# GH11080 - Check monotonic-ness before sort an index
3161+
# if monotonic (already sorted), return None or copy() according to 'inplace'
3162+
if (ascending and labels.is_monotonic_increasing) or \
3163+
(not ascending and labels.is_monotonic_decreasing):
3164+
if inplace:
3165+
return
3166+
else:
3167+
return self.copy()
3168+
31603169
indexer = _nargsort(labels, kind=kind, ascending=ascending,
31613170
na_position=na_position)
31623171

0 commit comments

Comments
 (0)