Skip to content

Commit 39ad1cb

Browse files
committed
BUG: Accept kwargs kind and na_position in Series.sort_index (GH13589)
Add tests for testing na_position and kind kwargs in Series.sort_index Fix minor issues with tests for Series.sort_index Update whatsnew entry; fix tests Update Series doc-string to mention new additions Remove update from Series doc-string Mention versionadded in Series.sort_index() doc-string Revert: Remove version tag from docstring Add test for invalid arguments
1 parent 1e61aed commit 39ad1cb

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ Other enhancements
513513
- ``Timestamp``, ``Period``, ``DatetimeIndex``, ``PeriodIndex`` and ``.dt`` accessor have gained a ``.is_leap_year`` property to check whether the date belongs to a leap year. (:issue:`13727`)
514514
- ``astype()`` will now accept a dict of column name to data types mapping as the ``dtype`` argument. (:issue:`12086`)
515515
- The ``pd.read_json`` and ``DataFrame.to_json`` has gained support for reading and writing json lines with ``lines`` option see :ref:`Line delimited json <io.jsonl>` (:issue:`9180`)
516+
- `Series.sort_index`` will now accept kwargs ``kind`` and ``na_position`` (:issue:`13589`)
516517

517518
.. _whatsnew_0190.api:
518519

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2022,16 +2022,16 @@ def sort_values(self, by, axis=0, ascending=True, inplace=False,
20222022
Sort ascending vs. descending
20232023
inplace : bool, default False
20242024
if True, perform operation in-place
2025+
sort_remaining : bool, default True
2026+
if true and sorting by level and index is multilevel, sort by other
2027+
levels too (in order) after sorting by specified level
20252028
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
20262029
Choice of sorting algorithm. See also ndarray.np.sort for more
20272030
information. `mergesort` is the only stable algorithm. For
20282031
DataFrames, this option is only applied when sorting on a single
20292032
column or label.
20302033
na_position : {'first', 'last'}, default 'last'
20312034
`first` puts NaNs at the beginning, `last` puts NaNs at the end
2032-
sort_remaining : bool, default True
2033-
if true and sorting by level and index is multilevel, sort by other
2034-
levels too (in order) after sorting by specified level
20352035
20362036
Returns
20372037
-------

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
@@ -1782,12 +1782,15 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
17821782
sort_remaining=sort_remaining)
17831783
elif isinstance(index, MultiIndex):
17841784
from pandas.core.groupby import _lexsort_indexer
1785-
indexer = _lexsort_indexer(index.labels, orders=ascending)
1785+
indexer = _lexsort_indexer(index.labels, orders=ascending,
1786+
na_position=na_position)
17861787
indexer = _ensure_platform_int(indexer)
17871788
new_index = index.take(indexer)
17881789
else:
17891790
new_index, indexer = index.sort_values(return_indexer=True,
1790-
ascending=ascending)
1791+
ascending=ascending,
1792+
kind=kind,
1793+
na_position=na_position)
17911794

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

pandas/tests/series/test_sorting.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from pandas import (DataFrame, Series, MultiIndex)
77

8-
from pandas.util.testing import (assert_series_equal, assert_almost_equal)
8+
from pandas.util.testing import (assert_series_equal, assert_almost_equal, assertRaisesRegexp)
99
import pandas.util.testing as tm
1010

1111
from .common import TestData
@@ -144,3 +144,22 @@ 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+
def test_sort_index_nan(self):
149+
150+
# GH13729
151+
ser = Series(['A', np.nan, 'C', 'D'], [1, 2, 0, np.nan])
152+
153+
# na_position='last', kind='quicksort'
154+
result = ser.sort_index(kind='quicksort', na_position='last')
155+
expected = Series(['C', 'A', np.nan, 'D'], [0, 1, 2, np.nan])
156+
assert_series_equal(result, expected)
157+
158+
# na_position='first'
159+
result = ser.sort_index(na_position='first')
160+
expected = Series(['D', 'C', 'A', np.nan], [np.nan, 0, 1, 2])
161+
assert_series_equal(result, expected)
162+
163+
# invalid argument
164+
assertRaisesRegexp(TypeError, 'got an unexpected keyword argument',
165+
ser.sort_index, arg='first')

0 commit comments

Comments
 (0)