Skip to content

BUG: sort_index(1, ignore_index=True, ascending=False) #57297

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 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ Styler

Other
^^^^^
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)


.. ***DO NOT USE THIS SECTION***

-
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5080,7 +5080,10 @@ def sort_index(
result = self.copy(deep=None)

if ignore_index:
result.index = default_index(len(self))
if axis == 1:
result.columns = default_index(len(self.columns))
else:
result.index = default_index(len(self))
if inplace:
return None
else:
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/frame/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,23 @@ def test_axis_columns_ignore_index():
tm.assert_frame_equal(result, expected)


def test_axis_columns_ignore_index_ascending_false():
# GH 57293
df = DataFrame(
{
"b": [1.0, 3.0, np.nan],
"a": [1, 4, 3],
1: ["a", "b", "c"],
"e": [3, 1, 4],
"d": [1, 2, 8],
}
).set_index(["b", "a", 1])
result = df.sort_index(axis="columns", ignore_index=True, ascending=False)
expected = df.copy()
expected.columns = RangeIndex(2)
tm.assert_frame_equal(result, expected)


def test_sort_index_stable_sort():
# GH 57151
df = DataFrame(
Expand Down