Skip to content

Commit 2e881c3

Browse files
authored
BUG: Index.sort_values with key (pandas-dev#54179)
1 parent fcb3357 commit 2e881c3

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

doc/source/whatsnew/v2.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -633,11 +633,13 @@ Other
633633
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)
634634
- Bug in :meth:`DataFrame.shift` and :meth:`Series.shift` and :meth:`DataFrameGroupBy.shift` when passing both "freq" and "fill_value" silently ignoring "fill_value" instead of raising ``ValueError`` (:issue:`53832`)
635635
- Bug in :meth:`DataFrame.shift` with ``axis=1`` on a :class:`DataFrame` with a single :class:`ExtensionDtype` column giving incorrect results (:issue:`53832`)
636+
- Bug in :meth:`Index.sort_values` when a ``key`` is passed (:issue:`52764`)
636637
- Bug in :meth:`Series.align`, :meth:`DataFrame.align`, :meth:`Series.reindex`, :meth:`DataFrame.reindex`, :meth:`Series.interpolate`, :meth:`DataFrame.interpolate`, incorrectly failing to raise with method="asfreq" (:issue:`53620`)
637638
- Bug in :meth:`Series.map` when giving a callable to an empty series, the returned series had ``object`` dtype. It now keeps the original dtype (:issue:`52384`)
638639
- Bug in :meth:`Series.memory_usage` when ``deep=True`` throw an error with Series of objects and the returned value is incorrect, as it does not take into account GC corrections (:issue:`51858`)
639640
- Bug in :meth:`period_range` the default behavior when freq was not passed as an argument was incorrect(:issue:`53687`)
640641
- Fixed incorrect ``__name__`` attribute of ``pandas._libs.json`` (:issue:`52898`)
642+
-
641643

642644
.. ***DO NOT USE THIS SECTION***
643645

pandas/core/indexes/base.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -5820,15 +5820,14 @@ def sort_values(
58205820
>>> idx.sort_values(ascending=False, return_indexer=True)
58215821
(Index([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
58225822
"""
5823-
idx = cast(Index, ensure_key_mapped(self, key))
5824-
58255823
# GH 35584. Sort missing values according to na_position kwarg
58265824
# ignore na_position for MultiIndex
58275825
if not isinstance(self, ABCMultiIndex):
58285826
_as = nargsort(
5829-
items=idx, ascending=ascending, na_position=na_position, key=key
5827+
items=self, ascending=ascending, na_position=na_position, key=key
58305828
)
58315829
else:
5830+
idx = cast(Index, ensure_key_mapped(self, key))
58325831
_as = idx.argsort(na_position=na_position)
58335832
if not ascending:
58345833
_as = _as[::-1]

pandas/tests/indexes/ranges/test_range.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,18 @@ def test_isin_range(self, base):
590590
tm.assert_numpy_array_equal(result, expected)
591591

592592
def test_sort_values_key(self):
593-
# GH#43666
593+
# GH#43666, GH#52764
594594
sort_order = {8: 2, 6: 0, 4: 8, 2: 10, 0: 12}
595595
values = RangeIndex(0, 10, 2)
596596
result = values.sort_values(key=lambda x: x.map(sort_order))
597-
expected = Index([4, 8, 6, 0, 2], dtype="int64")
597+
expected = Index([6, 8, 4, 2, 0], dtype="int64")
598598
tm.assert_index_equal(result, expected, check_exact=True)
599599

600+
# check this matches the Series.sort_values behavior
601+
ser = values.to_series()
602+
result2 = ser.sort_values(key=lambda x: x.map(sort_order))
603+
tm.assert_series_equal(result2, expected.to_series(), check_exact=True)
604+
600605
def test_range_index_rsub_by_const(self):
601606
# GH#53255
602607
result = 3 - RangeIndex(0, 4, 1)

0 commit comments

Comments
 (0)