Skip to content

Commit 52112d1

Browse files
authored
DOC: Corrected Out-of-date list of sort algorithms (#38503)
1 parent 4cf561f commit 52112d1

File tree

7 files changed

+29
-42
lines changed

7 files changed

+29
-42
lines changed

pandas/core/arrays/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def argsort(
561561
ascending : bool, default True
562562
Whether the indices should result in an ascending
563563
or descending sort.
564-
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
564+
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
565565
Sorting algorithm.
566566
*args, **kwargs:
567567
Passed through to :func:`numpy.argsort`.

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ def argsort(self, ascending=True, kind="quicksort", **kwargs):
14881488
ascending : bool, default True
14891489
Whether the indices should result in an ascending
14901490
or descending sort.
1491-
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
1491+
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
14921492
Sorting algorithm.
14931493
**kwargs:
14941494
passed through to :func:`numpy.argsort`.

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5526,9 +5526,9 @@ def sort_index(
55265526
sort direction can be controlled for each level individually.
55275527
inplace : bool, default False
55285528
If True, perform operation in-place.
5529-
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
5530-
Choice of sorting algorithm. See also ndarray.np.sort for more
5531-
information. `mergesort` is the only stable algorithm. For
5529+
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort'
5530+
Choice of sorting algorithm. See also :func:`numpy.sort` for more
5531+
information. `mergesort` and `stable` are the only stable algorithms. For
55325532
DataFrames, this option is only applied when sorting on a single
55335533
column or label.
55345534
na_position : {'first', 'last'}, default 'last'

pandas/core/generic.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4383,11 +4383,11 @@ def sort_values(
43834383
the by.
43844384
inplace : bool, default False
43854385
If True, perform operation in-place.
4386-
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
4387-
Choice of sorting algorithm. See also ndarray.np.sort for more
4388-
information. `mergesort` is the only stable algorithm. For
4389-
DataFrames, if sorting by multiple columns or labels, this
4390-
argument is ignored, defaulting to a stable sorting algorithm.
4386+
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort'
4387+
Choice of sorting algorithm. See also :func:`numpy.sort` for more
4388+
information. `mergesort` and `stable` are the only stable algorithms. For
4389+
DataFrames, this option is only applied when sorting on a single
4390+
column or label.
43914391
na_position : {'first', 'last'}, default 'last'
43924392
Puts NaNs at the beginning if `first`; `last` puts NaNs at the
43934393
end.

pandas/core/series.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -3066,9 +3066,9 @@ def sort_values(
30663066
If True, sort values in ascending order, otherwise descending.
30673067
inplace : bool, default False
30683068
If True, perform operation in-place.
3069-
kind : {'quicksort', 'mergesort' or 'heapsort'}, default 'quicksort'
3069+
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort'
30703070
Choice of sorting algorithm. See also :func:`numpy.sort` for more
3071-
information. 'mergesort' is the only stable algorithm.
3071+
information. 'mergesort' and 'stable' are the only stable algorithms.
30723072
na_position : {'first' or 'last'}, default 'last'
30733073
Argument 'first' puts NaNs at the beginning, 'last' puts NaNs at
30743074
the end.
@@ -3279,9 +3279,9 @@ def sort_index(
32793279
sort direction can be controlled for each level individually.
32803280
inplace : bool, default False
32813281
If True, perform operation in-place.
3282-
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
3282+
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort'
32833283
Choice of sorting algorithm. See also :func:`numpy.sort` for more
3284-
information. 'mergesort' is the only stable algorithm. For
3284+
information. 'mergesort' and 'stable' are the only stable algorithms. For
32853285
DataFrames, this option is only applied when sorting on a single
32863286
column or label.
32873287
na_position : {'first', 'last'}, default 'last'
@@ -3420,9 +3420,9 @@ def argsort(self, axis=0, kind="quicksort", order=None) -> "Series":
34203420
----------
34213421
axis : {0 or "index"}
34223422
Has no effect but is accepted for compatibility with numpy.
3423-
kind : {'mergesort', 'quicksort', 'heapsort'}, default 'quicksort'
3424-
Choice of sorting algorithm. See np.sort for more
3425-
information. 'mergesort' is the only stable algorithm.
3423+
kind : {'mergesort', 'quicksort', 'heapsort', 'stable'}, default 'quicksort'
3424+
Choice of sorting algorithm. See :func:`numpy.sort` for more
3425+
information. 'mergesort' and 'stable' are the only stable algorithms.
34263426
order : None
34273427
Has no effect but is accepted for compatibility with numpy.
34283428

pandas/core/sorting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def get_indexer_indexer(
5454
target : Index
5555
level : int or level name or list of ints or list of level names
5656
ascending : bool or list of bools, default True
57-
kind : {'quicksort', 'mergesort', 'heapsort'}, default 'quicksort'
57+
kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort'
5858
na_position : {'first', 'last'}, default 'last'
5959
sort_remaining : bool, default True
6060
key : callable, optional

pandas/tests/series/methods/test_sort_index.py

+11-24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import pandas._testing as tm
88

99

10+
@pytest.fixture(params=["quicksort", "mergesort", "heapsort", "stable"])
11+
def sort_kind(request):
12+
return request.param
13+
14+
1015
class TestSeriesSortIndex:
1116
def test_sort_index_name(self, datetime_series):
1217
result = datetime_series.sort_index(ascending=False)
@@ -104,18 +109,12 @@ def test_sort_index_multiindex(self, level):
104109
res = s.sort_index(level=level, sort_remaining=False)
105110
tm.assert_series_equal(s, res)
106111

107-
def test_sort_index_kind(self):
112+
def test_sort_index_kind(self, sort_kind):
108113
# GH#14444 & GH#13589: Add support for sort algo choosing
109114
series = Series(index=[3, 2, 1, 4, 3], dtype=object)
110115
expected_series = Series(index=[1, 2, 3, 3, 4], dtype=object)
111116

112-
index_sorted_series = series.sort_index(kind="mergesort")
113-
tm.assert_series_equal(expected_series, index_sorted_series)
114-
115-
index_sorted_series = series.sort_index(kind="quicksort")
116-
tm.assert_series_equal(expected_series, index_sorted_series)
117-
118-
index_sorted_series = series.sort_index(kind="heapsort")
117+
index_sorted_series = series.sort_index(kind=sort_kind)
119118
tm.assert_series_equal(expected_series, index_sorted_series)
120119

121120
def test_sort_index_na_position(self):
@@ -251,32 +250,20 @@ def test_sort_index_key_int(self):
251250
result = series.sort_index(key=lambda x: 2 * x)
252251
tm.assert_series_equal(result, series)
253252

254-
def test_sort_index_kind_key(self, sort_by_key):
253+
def test_sort_index_kind_key(self, sort_kind, sort_by_key):
255254
# GH #14444 & #13589: Add support for sort algo choosing
256255
series = Series(index=[3, 2, 1, 4, 3], dtype=object)
257256
expected_series = Series(index=[1, 2, 3, 3, 4], dtype=object)
258257

259-
index_sorted_series = series.sort_index(kind="mergesort", key=sort_by_key)
260-
tm.assert_series_equal(expected_series, index_sorted_series)
261-
262-
index_sorted_series = series.sort_index(kind="quicksort", key=sort_by_key)
263-
tm.assert_series_equal(expected_series, index_sorted_series)
264-
265-
index_sorted_series = series.sort_index(kind="heapsort", key=sort_by_key)
258+
index_sorted_series = series.sort_index(kind=sort_kind, key=sort_by_key)
266259
tm.assert_series_equal(expected_series, index_sorted_series)
267260

268-
def test_sort_index_kind_neg_key(self):
261+
def test_sort_index_kind_neg_key(self, sort_kind):
269262
# GH #14444 & #13589: Add support for sort algo choosing
270263
series = Series(index=[3, 2, 1, 4, 3], dtype=object)
271264
expected_series = Series(index=[4, 3, 3, 2, 1], dtype=object)
272265

273-
index_sorted_series = series.sort_index(kind="mergesort", key=lambda x: -x)
274-
tm.assert_series_equal(expected_series, index_sorted_series)
275-
276-
index_sorted_series = series.sort_index(kind="quicksort", key=lambda x: -x)
277-
tm.assert_series_equal(expected_series, index_sorted_series)
278-
279-
index_sorted_series = series.sort_index(kind="heapsort", key=lambda x: -x)
266+
index_sorted_series = series.sort_index(kind=sort_kind, key=lambda x: -x)
280267
tm.assert_series_equal(expected_series, index_sorted_series)
281268

282269
def test_sort_index_na_position_key(self, sort_by_key):

0 commit comments

Comments
 (0)