Skip to content

Commit

Permalink
TST: sort index with sliced MultiIndex (#55473)
Browse files Browse the repository at this point in the history
* TST: sort index with sliced MultiIndex

* whatsnew
  • Loading branch information
lukemanley authored Oct 10, 2023
1 parent 2a65fdd commit f7b49d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Fixed regressions
- 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`)
- Fixed regression in :meth:`DataFrame.join` where result has missing values and dtype is arrow backed string (:issue:`55348`)
- Fixed regression in :meth:`DataFrame.resample` which was extrapolating back to ``origin`` when ``origin`` was outside its bounds (:issue:`55064`)
- Fixed regression in :meth:`DataFrame.sort_index` which was not sorting correctly when the index was a sliced :class:`MultiIndex` (:issue:`55379`)

.. ---------------------------------------------------------------------------
.. _whatsnew_212.bug_fixes:
Expand Down
39 changes: 39 additions & 0 deletions pandas/tests/frame/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,3 +955,42 @@ def test_sort_index_multiindex_sort_remaining(self, ascending):
)

tm.assert_frame_equal(result, expected)


def test_sort_index_with_sliced_multiindex():
# GH 55379
mi = MultiIndex.from_tuples(
[
("a", "10"),
("a", "18"),
("a", "25"),
("b", "16"),
("b", "26"),
("a", "45"),
("b", "28"),
("a", "5"),
("a", "50"),
("a", "51"),
("b", "4"),
],
names=["group", "str"],
)

df = DataFrame({"x": range(len(mi))}, index=mi)
result = df.iloc[0:6].sort_index()

expected = DataFrame(
{"x": [0, 1, 2, 5, 3, 4]},
index=MultiIndex.from_tuples(
[
("a", "10"),
("a", "18"),
("a", "25"),
("a", "45"),
("b", "16"),
("b", "26"),
],
names=["group", "str"],
),
)
tm.assert_frame_equal(result, expected)

0 comments on commit f7b49d8

Please sign in to comment.