Skip to content

Commit f7b49d8

Browse files
authored
TST: sort index with sliced MultiIndex (#55473)
* TST: sort index with sliced MultiIndex * whatsnew
1 parent 2a65fdd commit f7b49d8

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed regressions
1616
- Fixed bug where PDEP-6 warning about setting an item of an incompatible dtype was being shown when creating a new conditional column (:issue:`55025`)
1717
- Fixed regression in :meth:`DataFrame.join` where result has missing values and dtype is arrow backed string (:issue:`55348`)
1818
- Fixed regression in :meth:`DataFrame.resample` which was extrapolating back to ``origin`` when ``origin`` was outside its bounds (:issue:`55064`)
19+
- Fixed regression in :meth:`DataFrame.sort_index` which was not sorting correctly when the index was a sliced :class:`MultiIndex` (:issue:`55379`)
1920

2021
.. ---------------------------------------------------------------------------
2122
.. _whatsnew_212.bug_fixes:

pandas/tests/frame/methods/test_sort_index.py

+39
Original file line numberDiff line numberDiff line change
@@ -955,3 +955,42 @@ def test_sort_index_multiindex_sort_remaining(self, ascending):
955955
)
956956

957957
tm.assert_frame_equal(result, expected)
958+
959+
960+
def test_sort_index_with_sliced_multiindex():
961+
# GH 55379
962+
mi = MultiIndex.from_tuples(
963+
[
964+
("a", "10"),
965+
("a", "18"),
966+
("a", "25"),
967+
("b", "16"),
968+
("b", "26"),
969+
("a", "45"),
970+
("b", "28"),
971+
("a", "5"),
972+
("a", "50"),
973+
("a", "51"),
974+
("b", "4"),
975+
],
976+
names=["group", "str"],
977+
)
978+
979+
df = DataFrame({"x": range(len(mi))}, index=mi)
980+
result = df.iloc[0:6].sort_index()
981+
982+
expected = DataFrame(
983+
{"x": [0, 1, 2, 5, 3, 4]},
984+
index=MultiIndex.from_tuples(
985+
[
986+
("a", "10"),
987+
("a", "18"),
988+
("a", "25"),
989+
("a", "45"),
990+
("b", "16"),
991+
("b", "26"),
992+
],
993+
names=["group", "str"],
994+
),
995+
)
996+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)