Skip to content

Added test for sort_index parameter multiindex 'sort_remaining' = False #53076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 6, 2023
Merged
31 changes: 31 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,34 @@ 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, testing multiindex sort_remaining=false consistent behavior
Copy link
Member

Choose a reason for hiding this comment

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

You can remove the comment except the gh reference

# for ascending = True, ascending = False
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")]
),
)

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

if ascending:
df_expected = DataFrame(
Copy link
Member

Choose a reason for hiding this comment

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

can you call result and expected?

{"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:
df_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")]
),
)

# Assert that the resulting DataFrame is sorted correctly
Copy link
Member

Choose a reason for hiding this comment

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

Same, can remove comment

tm.assert_frame_equal(df_sorted, df_expected)