Skip to content

Commit 73bac83

Browse files
Fix DataFrame.sort_index to respect ignore_index on all axis (#14995)
This PR fixes `DataFrame.sort_index` to properly ignore indexes for all values of `axis`. This is fixed in pandas-2.2, hence xfailing the tests with a version check. Authors: - GALI PREM SAGAR (https://github.com/galipremsagar) Approvers: - Bradley Dice (https://github.com/bdice) URL: #14995
1 parent 285b836 commit 73bac83

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

python/cudf/cudf/core/indexed_frame.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,12 +2608,15 @@ def sort_index(
26082608
and self._data.multiindex
26092609
):
26102610
out._set_column_names_like(self)
2611+
if ignore_index:
2612+
out = out.reset_index(drop=True)
26112613
else:
26122614
labels = sorted(self._data.names, reverse=not ascending)
26132615
out = self[labels]
2616+
if ignore_index:
2617+
out._data.rangeindex = True
2618+
out._data.names = list(range(len(self._data.names)))
26142619

2615-
if ignore_index is True:
2616-
out = out.reset_index(drop=True)
26172620
return self._mimic_inplace(out, inplace=inplace)
26182621

26192622
def memory_usage(self, index=True, deep=False):

python/cudf/cudf/tests/test_dataframe.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525

2626
import cudf
2727
from cudf.api.extensions import no_default
28-
from cudf.core._compat import PANDAS_GE_200, PANDAS_GE_210, PANDAS_LT_203
28+
from cudf.core._compat import (
29+
PANDAS_GE_200,
30+
PANDAS_GE_210,
31+
PANDAS_GE_220,
32+
PANDAS_LT_203,
33+
)
2934
from cudf.core.buffer.spill_manager import get_global_manager
3035
from cudf.core.column import column
3136
from cudf.errors import MixedTypeError
@@ -3562,8 +3567,16 @@ def test_dataframe_empty_sort_index():
35623567
@pytest.mark.parametrize("inplace", [True, False])
35633568
@pytest.mark.parametrize("na_position", ["first", "last"])
35643569
def test_dataframe_sort_index(
3565-
index, axis, ascending, inplace, ignore_index, na_position
3570+
request, index, axis, ascending, inplace, ignore_index, na_position
35663571
):
3572+
request.applymarker(
3573+
pytest.mark.xfail(
3574+
condition=not PANDAS_GE_220
3575+
and axis in (1, "columns")
3576+
and ignore_index,
3577+
reason="Bug fixed in pandas-2.2",
3578+
)
3579+
)
35673580
pdf = pd.DataFrame(
35683581
{"b": [1, 3, 2], "a": [1, 4, 3], "c": [4, 1, 5]},
35693582
index=index,
@@ -3618,12 +3631,22 @@ def test_dataframe_mulitindex_sort_index(
36183631
):
36193632
request.applymarker(
36203633
pytest.mark.xfail(
3621-
condition=axis in (1, "columns")
3634+
condition=not PANDAS_GE_220
3635+
and axis in (1, "columns")
36223636
and ignore_index
36233637
and not (level is None and not ascending),
36243638
reason="https://github.com/pandas-dev/pandas/issues/56478",
36253639
)
36263640
)
3641+
request.applymarker(
3642+
pytest.mark.xfail(
3643+
condition=axis in (1, "columns")
3644+
and level is None
3645+
and not ascending
3646+
and ignore_index,
3647+
reason="https://github.com/pandas-dev/pandas/issues/57293",
3648+
)
3649+
)
36273650
pdf = pd.DataFrame(
36283651
{
36293652
"b": [1.0, 3.0, np.nan],

0 commit comments

Comments
 (0)