Skip to content

Commit bb5ca1a

Browse files
Backport PR #64386 on branch 3.0.x (BUG: fix sort_index AssertionError with RangeIndex and level parameter) (#64882)
Co-authored-by: Fadi Younes <37001921+repeating@users.noreply.github.com>
1 parent 6b478e4 commit bb5ca1a

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

doc/source/whatsnew/v3.0.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Enhancements
2121
Fixed regressions
2222
~~~~~~~~~~~~~~~~~
2323
- Fixed a regression in :func:`json_normalize` that caused top-level missing entries to raise a ``TypeError``. Missing entries are once again interpreted as empty records (:issue:`64188`)
24+
- Fixed regression in :meth:`DataFrame.sort_index` and :meth:`Series.sort_index` raising ``AssertionError`` when called with ``level=`` on a :class:`RangeIndex` (:issue:`64383`)
2425
- Fixed regression in :meth:`HDFStore.select` where the ``where`` clause on a datetime index silently returned empty results when the index had non-nanosecond resolution (:issue:`64310`)
2526
- Fixed regression in :meth:`Series.interpolate` where ``limit_direction="both"`` with ``limit`` greater than the Series length raised ``ValueError`` (:issue:`64322`)
2627

pandas/core/indexes/range.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,7 @@ def equals(self, other: object) -> bool:
772772
return self._range == other._range
773773
return super().equals(other)
774774

775-
# error: Signature of "sort_values" incompatible with supertype "Index"
776-
@overload # type: ignore[override]
775+
@overload
777776
def sort_values(
778777
self,
779778
*,
@@ -791,7 +790,7 @@ def sort_values(
791790
ascending: bool = ...,
792791
na_position: NaPosition = ...,
793792
key: Callable | None = ...,
794-
) -> tuple[Self, np.ndarray | RangeIndex]: ...
793+
) -> tuple[Self, np.ndarray]: ...
795794

796795
@overload
797796
def sort_values(
@@ -801,7 +800,7 @@ def sort_values(
801800
ascending: bool = ...,
802801
na_position: NaPosition = ...,
803802
key: Callable | None = ...,
804-
) -> Self | tuple[Self, np.ndarray | RangeIndex]: ...
803+
) -> Self | tuple[Self, np.ndarray]: ...
805804

806805
def sort_values(
807806
self,
@@ -810,7 +809,7 @@ def sort_values(
810809
ascending: bool = True,
811810
na_position: NaPosition = "last",
812811
key: Callable | None = None,
813-
) -> Self | tuple[Self, np.ndarray | RangeIndex]:
812+
) -> Self | tuple[Self, np.ndarray]:
814813
if key is not None:
815814
return super().sort_values(
816815
return_indexer=return_indexer,
@@ -831,10 +830,10 @@ def sort_values(
831830

832831
if return_indexer:
833832
if inverse_indexer:
834-
rng = range(len(self) - 1, -1, -1)
833+
indexer = np.arange(len(self) - 1, -1, -1, dtype=np.intp)
835834
else:
836-
rng = range(len(self))
837-
return sorted_index, RangeIndex(rng)
835+
indexer = np.arange(len(self), dtype=np.intp)
836+
return sorted_index, indexer
838837
else:
839838
return sorted_index
840839

pandas/tests/frame/methods/test_sort_index.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,13 @@ def test_sort_index_ascending_tuple(self, ascending):
815815

816816
tm.assert_frame_equal(result, expected)
817817

818+
def test_sort_index_level_on_range_index(self):
819+
# GH#64383: sort_index with level= on a RangeIndex raised AssertionError
820+
df = DataFrame({"a": [1, 2, 3]})
821+
df.index.names = ["foo"]
822+
result = df.sort_index(level="foo")
823+
tm.assert_frame_equal(result, df)
824+
818825

819826
class TestDataFrameSortIndexKey:
820827
def test_sort_multi_index_key(self):

0 commit comments

Comments
 (0)