Skip to content

Commit 72221b3

Browse files
Fix DataFrame.sort_index when a index is a MultiIndex (#14621)
This PR fixes sorting of a MultiIndex by removing an existing hard-coded na_position value that was based on ascending flag, essentially ignoring the user-passed parameter. On pandas_2.0_feature_branch: = 501 failed, 101106 passed, 2071 skipped, 786 xfailed, 312 xpassed, 20 errors in 1234.91s (0:20:34) = This PR: = 405 failed, 101034 passed, 2071 skipped, 954 xfailed, 312 xpassed, 20 errors in 1124.69s (0:18:44) =
1 parent 5f3ecd6 commit 72221b3

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

python/cudf/cudf/core/indexed_frame.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,8 +1611,6 @@ def sort_index(
16111611
idx = self.index
16121612
if isinstance(idx, MultiIndex):
16131613
if level is not None:
1614-
# Pandas doesn't handle na_position in case of MultiIndex.
1615-
na_position = "first" if ascending is True else "last"
16161614
if not is_list_like(level):
16171615
level = [level]
16181616
by = list(map(idx._get_level_label, level))

python/cudf/cudf/tests/test_dataframe.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,8 +3492,16 @@ def test_dataframe_sort_index(
34923492
@pytest.mark.parametrize("inplace", [True, False])
34933493
@pytest.mark.parametrize("na_position", ["first", "last"])
34943494
def test_dataframe_mulitindex_sort_index(
3495-
axis, level, ascending, inplace, ignore_index, na_position
3495+
request, axis, level, ascending, inplace, ignore_index, na_position
34963496
):
3497+
request.applymarker(
3498+
pytest.mark.xfail(
3499+
condition=axis in (1, "columns")
3500+
and ignore_index
3501+
and not (level is None and not ascending),
3502+
reason="https://github.com/pandas-dev/pandas/issues/56478",
3503+
)
3504+
)
34973505
pdf = pd.DataFrame(
34983506
{
34993507
"b": [1.0, 3.0, np.nan],
@@ -3505,17 +3513,14 @@ def test_dataframe_mulitindex_sort_index(
35053513
).set_index(["b", "a", 1])
35063514
gdf = cudf.DataFrame.from_pandas(pdf)
35073515

3508-
# ignore_index is supported in v.1.0
3509-
35103516
expected = pdf.sort_index(
35113517
axis=axis,
35123518
level=level,
35133519
ascending=ascending,
35143520
inplace=inplace,
35153521
na_position=na_position,
3522+
ignore_index=ignore_index,
35163523
)
3517-
if ignore_index is True:
3518-
expected = expected
35193524
got = gdf.sort_index(
35203525
axis=axis,
35213526
level=level,
@@ -3526,12 +3531,8 @@ def test_dataframe_mulitindex_sort_index(
35263531
)
35273532

35283533
if inplace is True:
3529-
if ignore_index is True:
3530-
pdf = pdf.reset_index(drop=True)
35313534
assert_eq(pdf, gdf)
35323535
else:
3533-
if ignore_index is True:
3534-
expected = expected.reset_index(drop=True)
35353536
assert_eq(expected, got)
35363537

35373538

0 commit comments

Comments
 (0)