From 2f9ef9ec0dfbe04b95acd69ba8327779856ef503 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 7 Feb 2024 11:40:15 -0800 Subject: [PATCH 1/2] BUG: sort_index(1, ignore_index=True, ascending=False) --- doc/source/whatsnew/v3.0.0.rst | 2 +- pandas/core/generic.py | 5 ++++- pandas/tests/frame/methods/test_sort_index.py | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 2c39318fa28b3..fcf268c15da95 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -242,7 +242,7 @@ Styler Other ^^^^^ - 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`) - +- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293it `) .. ***DO NOT USE THIS SECTION*** diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 93c2afab51d2c..1741c61e7195f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -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: diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index 1a5df0778ec52..d88daaff685aa 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -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( From 5ef264c7343ae8e528ffcccf58214875a7a57b6e Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 7 Feb 2024 12:16:08 -0800 Subject: [PATCH 2/2] Remove error --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index fcf268c15da95..503da28684d60 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -241,8 +241,8 @@ 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`) -- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293it `) .. ***DO NOT USE THIS SECTION***