Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Interval
Indexing
^^^^^^^^
- Bugs in setitem-with-expansion when adding new rows failing to keep the original dtype in some cases (:issue:`32346`, :issue:`15231`, :issue:`47503`, :issue:`6485`, :issue:`25383`, :issue:`52235`, :issue:`17026`, :issue:`56010`)
- Fixed bug in :meth:`DataFrame.sort_index` and :meth:`Series.sort_index` where passing the ``level`` keyword with a single-level :class:`RangeIndex` raised an ``AssertionError`` (:issue:`64383`)
-

Missing
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def get_indexer_indexer(
sort_remaining=sort_remaining,
na_position=na_position,
)
if isinstance(indexer, ABCRangeIndex):
indexer = indexer._data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a fastpath available if we know the indexer is a RangeIndex?

elif (np.all(ascending) and target.is_monotonic_increasing) or (
not np.any(ascending) and target.is_monotonic_decreasing
):
Expand Down
46 changes: 46 additions & 0 deletions pandas/tests/frame/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,3 +1030,49 @@ def test_sort_index_stable_sort():
columns=["dt", "value"],
).set_index(["dt"])
tm.assert_frame_equal(result, expected)


def test_sort_index_rangeindex_with_level_keyword():
# GH#64383 - sort_index with level keyword on single-level RangeIndex
# should not raise AssertionError
df = DataFrame({"a": [1, 2, 3], "foo": [0, 1, 2]}).set_index(["foo"], drop=True)
result = df.sort_index(level="foo")
expected = DataFrame({"a": [1, 2, 3]}, index=RangeIndex(3, name="foo"))
tm.assert_frame_equal(result, expected)

# Also test when the index is already a RangeIndex with a named index
df2 = DataFrame({"a": [1, 2, 3]})
df2.index.names = ["foo"]
result2 = df2.sort_index(level="foo")
expected2 = DataFrame({"a": [1, 2, 3]}, index=RangeIndex(3, name="foo"))
tm.assert_frame_equal(result2, expected2)

# Test unsorted RangeIndex
df3 = DataFrame({"a": [1, 2, 3], "foo": [1, 0, 2]}).set_index(["foo"], drop=True)
result3 = df3.sort_index(level="foo")
expected3 = DataFrame(
{"a": [2, 1, 3]},
index=pd.Index([0, 1, 2], name="foo"),
)
tm.assert_frame_equal(result3, expected3)

# Test reversed RangeIndex
df4 = DataFrame(
{"a": [1, 2, 3]}, index=RangeIndex(start=2, stop=-1, step=-1, name="foo")
)
result4 = df4.sort_index(level="foo")
expected4 = DataFrame(
{"a": [3, 2, 1]},
index=pd.Index([0, 1, 2], name="foo"),
)
tm.assert_frame_equal(result4, expected4)

# Test ascending=False
df5 = DataFrame({"a": [1, 2, 3]})
df5.index.names = ["foo"]
result5 = df5.sort_index(level="foo", ascending=False)
expected5 = DataFrame(
{"a": [3, 2, 1]},
index=pd.Index([2, 1, 0], name="foo"),
)
tm.assert_frame_equal(result5, expected5)
Loading