Skip to content

Commit

Permalink
TST: Added test for sort_index parameter multiindex 'sort_remaining' …
Browse files Browse the repository at this point in the history
…= False (pandas-dev#53076)
  • Loading branch information
NoyHanan authored and im-vinicius committed Jul 8, 2023
1 parent 00d140a commit ab68550
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pandas/tests/frame/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,3 +914,32 @@ def test_sort_index_na_position(self):
expected = df.copy()
result = df.sort_index(level=[0, 1], na_position="last")
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("ascending", [True, False])
def test_sort_index_multiindex_sort_remaining(self, ascending):
# GH #24247
df = DataFrame(
{"A": [1, 2, 3, 4, 5], "B": [10, 20, 30, 40, 50]},
index=MultiIndex.from_tuples(
[("a", "x"), ("a", "y"), ("b", "x"), ("b", "y"), ("c", "x")]
),
)

result = df.sort_index(level=1, sort_remaining=False, ascending=ascending)

if ascending:
expected = DataFrame(
{"A": [1, 3, 5, 2, 4], "B": [10, 30, 50, 20, 40]},
index=MultiIndex.from_tuples(
[("a", "x"), ("b", "x"), ("c", "x"), ("a", "y"), ("b", "y")]
),
)
else:
expected = DataFrame(
{"A": [2, 4, 1, 3, 5], "B": [20, 40, 10, 30, 50]},
index=MultiIndex.from_tuples(
[("a", "y"), ("b", "y"), ("a", "x"), ("b", "x"), ("c", "x")]
),
)

tm.assert_frame_equal(result, expected)

0 comments on commit ab68550

Please sign in to comment.