Skip to content

Commit e69cbe4

Browse files
BUG Series.sort_index does not accept parameters kind and na_position
1 parent 7cad3f1 commit e69cbe4

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

doc/source/whatsnew/v0.19.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ Bug Fixes
4848
- Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`)
4949
- Bug in ``DataFrame.to_json`` where ``lines=True`` and a value contained a ``}`` character (:issue:`14391`)
5050
- Bug in ``df.groupby`` causing an ``AttributeError`` when grouping a single index frame by a column and the index level (:issue`14327`)
51+
- Bug in ``Series.sort_index`` where parameters ``kind`` and ``na_position`` did not exist (:issue:`13589` & :issue:`14444`)

pandas/core/frame.py

-10
Original file line numberDiff line numberDiff line change
@@ -3323,16 +3323,6 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
33233323
else:
33243324
from pandas.core.groupby import _nargsort
33253325

3326-
# GH11080 - Check monotonic-ness before sort an index
3327-
# if monotonic (already sorted), return None or copy() according
3328-
# to 'inplace'
3329-
if ((ascending and labels.is_monotonic_increasing) or
3330-
(not ascending and labels.is_monotonic_decreasing)):
3331-
if inplace:
3332-
return
3333-
else:
3334-
return self.copy()
3335-
33363326
indexer = _nargsort(labels, kind=kind, ascending=ascending,
33373327
na_position=na_position)
33383328

pandas/core/groupby.py

+5
Original file line numberDiff line numberDiff line change
@@ -4280,6 +4280,11 @@ def _nargsort(items, kind='quicksort', ascending=True, na_position='last'):
42804280
if is_categorical_dtype(items):
42814281
return items.argsort(ascending=ascending)
42824282

4283+
# # GH11080 - Check monotonic-ness before sort an index
4284+
if ((ascending and all(x <= y for x, y in zip(items, items[1:]))) or
4285+
(not ascending and all(x >= y for x, y in zip(items, items[1:])))):
4286+
return np.arange(len(items))
4287+
42834288
items = np.asanyarray(items)
42844289
idx = np.arange(len(items))
42854290
mask = isnull(items)

pandas/core/series.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ def _try_kind_sort(arr):
17731773

17741774
@Appender(generic._shared_docs['sort_index'] % _shared_doc_kwargs)
17751775
def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
1776-
sort_remaining=True):
1776+
kind='quicksort', na_position='last', sort_remaining=True):
17771777

17781778
axis = self._get_axis_number(axis)
17791779
index = self.index
@@ -1786,8 +1786,11 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
17861786
indexer = _ensure_platform_int(indexer)
17871787
new_index = index.take(indexer)
17881788
else:
1789-
new_index, indexer = index.sort_values(return_indexer=True,
1790-
ascending=ascending)
1789+
from pandas.core.groupby import _nargsort
1790+
1791+
indexer = _nargsort(index, kind=kind, ascending=ascending,
1792+
na_position=na_position)
1793+
new_index = index.take(indexer)
17911794

17921795
new_values = self._values.take(indexer)
17931796
result = self._constructor(new_values, index=new_index)

pandas/tests/series/test_sorting.py

+25
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,28 @@ def test_sort_index_multiindex(self):
144144
# rows share same level='A': sort has no effect without remaining lvls
145145
res = s.sort_index(level='A', sort_remaining=False)
146146
assert_series_equal(s, res)
147+
148+
# GH #14444 & #13589: Add support for sort algo choosing
149+
def test_sort_index_kind(self):
150+
series = Series(index=[3, 2, 1, 4, 3])
151+
expected_series = Series(index=[1, 2, 3, 3, 4])
152+
153+
index_sorted_series = series.sort_index(kind='mergesort')
154+
assert_series_equal(expected_series, index_sorted_series)
155+
156+
index_sorted_series = series.sort_index(kind='quicksort')
157+
assert_series_equal(expected_series, index_sorted_series)
158+
159+
index_sorted_series = series.sort_index(kind='heapsort')
160+
assert_series_equal(expected_series, index_sorted_series)
161+
162+
def test_sort_index_na_position(self):
163+
series = Series(index=[3, 2, 1, 4, 3, np.nan])
164+
165+
expected_series_first = Series(index=[np.nan, 1, 2, 3, 3, 4])
166+
index_sorted_series = series.sort_index(na_position='first')
167+
assert_series_equal(expected_series_first, index_sorted_series)
168+
169+
expected_series_last = Series(index=[1, 2, 3, 3, 4, np.nan])
170+
index_sorted_series = series.sort_index(na_position='last')
171+
assert_series_equal(expected_series_last, index_sorted_series)

0 commit comments

Comments
 (0)