Skip to content

Commit 271f8f2

Browse files
authored
BUG: sort_index(1, ignore_index=True, ascending=False) (#57297)
1 parent 6479529 commit 271f8f2

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ Styler
245245

246246
Other
247247
^^^^^
248+
- 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`)
248249
- 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`)
249250

250-
251251
.. ***DO NOT USE THIS SECTION***
252252
253253
-

pandas/core/generic.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5102,7 +5102,10 @@ def sort_index(
51025102
result = self.copy(deep=None)
51035103

51045104
if ignore_index:
5105-
result.index = default_index(len(self))
5105+
if axis == 1:
5106+
result.columns = default_index(len(self.columns))
5107+
else:
5108+
result.index = default_index(len(self))
51065109
if inplace:
51075110
return None
51085111
else:

pandas/tests/frame/methods/test_sort_index.py

+17
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,23 @@ def test_axis_columns_ignore_index():
10031003
tm.assert_frame_equal(result, expected)
10041004

10051005

1006+
def test_axis_columns_ignore_index_ascending_false():
1007+
# GH 57293
1008+
df = DataFrame(
1009+
{
1010+
"b": [1.0, 3.0, np.nan],
1011+
"a": [1, 4, 3],
1012+
1: ["a", "b", "c"],
1013+
"e": [3, 1, 4],
1014+
"d": [1, 2, 8],
1015+
}
1016+
).set_index(["b", "a", 1])
1017+
result = df.sort_index(axis="columns", ignore_index=True, ascending=False)
1018+
expected = df.copy()
1019+
expected.columns = RangeIndex(2)
1020+
tm.assert_frame_equal(result, expected)
1021+
1022+
10061023
def test_sort_index_stable_sort():
10071024
# GH 57151
10081025
df = DataFrame(

0 commit comments

Comments
 (0)