diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index fc49177a4736b..b019c5b3d30e8 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -412,6 +412,7 @@ Reshaping - Bug in :meth:`DataFrame.join` not assigning values correctly when having :class:`MultiIndex` where at least one dimension is from dtype ``Categorical`` with non-alphabetically sorted categories (:issue:`38502`) - :meth:`Series.value_counts` and :meth:`Series.mode` return consistent keys in original order (:issue:`12679`, :issue:`11227` and :issue:`39007`) - Bug in :meth:`DataFrame.apply` would give incorrect results when used with a string argument and ``axis=1`` when the axis argument was not supported and now raises a ``ValueError`` instead (:issue:`39211`) +- Bug in :meth:`DataFrame.sort_values` not reshaping index correctly after sorting on columns, when ``ignore_index=True`` (:issue:`39464`) - Bug in :meth:`DataFrame.append` returning incorrect dtypes with combinations of ``ExtensionDtype`` dtypes (:issue:`39454`) Sparse diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 57eb384d79868..6357b8feb348b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5560,7 +5560,9 @@ def sort_values( # type: ignore[override] ) if ignore_index: - new_data.set_axis(1, ibase.default_index(len(indexer))) + new_data.set_axis( + self._get_block_manager_axis(axis), ibase.default_index(len(indexer)) + ) result = self._constructor(new_data) if inplace: diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index 86c9e2f5ffe52..053684ba08484 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -579,6 +579,14 @@ def test_sort_values_item_cache(self, using_array_manager): assert df.iloc[0, 0] == df["A"][0] + def test_sort_values_reshaping(self): + # GH 39426 + values = list(range(21)) + expected = DataFrame([values], columns=values) + df = expected.sort_values(expected.index[0], axis=1, ignore_index=True) + + tm.assert_frame_equal(df, expected) + class TestDataFrameSortKey: # test key sorting (issue 27237) def test_sort_values_inplace_key(self, sort_by_key):