From ad9df647b6afd48f62bf5b961697e37eb4d9d7c5 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 10 Apr 2023 17:54:09 +0200 Subject: [PATCH 1/4] REGR: Fix regression in sort_values not resetting index --- doc/source/whatsnew/v2.0.1.rst | 1 + pandas/core/frame.py | 8 ++++++-- pandas/tests/frame/methods/test_sort_values.py | 7 +++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index caf237fb15163..12134fe79df6d 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -15,6 +15,7 @@ Fixed regressions ~~~~~~~~~~~~~~~~~ - Fixed regression for subclassed Series when constructing from a dictionary (:issue:`52445`) - Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`) +- Fixed regression in :meth:`DataFrame.sort_values` not resetting index when :class:`DataFrame` is already sorted and ``ignore_index=True`` (:issue:`52553`) .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index fa36fef29979c..dc3f8ca1d4105 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6771,10 +6771,14 @@ def sort_values( return self.copy(deep=None) if is_range_indexer(indexer, len(indexer)): + result = self.copy(deep=None) + if ignore_index: + result.index = default_index(len(result)) + if inplace: - return self._update_inplace(self) + return self._update_inplace(result) else: - return self.copy(deep=None) + return result new_data = self._mgr.take( indexer, axis=self._get_block_manager_axis(axis), verify=False diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index 9092f30e0e4d0..e2877acbdd040 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -630,6 +630,13 @@ def test_sort_values_no_by_inplace(self): tm.assert_frame_equal(df, expected) assert result is None + def test_sort_values_no_op_reset_index(self): + # GH#52553 + df = DataFrame({"A": [10, 20], "B": [1, 5]}, index=[2, 3]) + result = df.sort_values(by="A", ignore_index=True) + expected = DataFrame({"A": [10, 20], "B": [1, 5]}) + tm.assert_frame_equal(result, expected) + class TestDataFrameSortKey: # test key sorting (issue 27237) def test_sort_values_inplace_key(self, sort_by_key): From 6da140ed323b26881dd15a9a1499e7ae79871d52 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 10 Apr 2023 18:14:17 +0200 Subject: [PATCH 2/4] Fix --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index dc3f8ca1d4105..e33d24681a422 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6771,7 +6771,7 @@ def sort_values( return self.copy(deep=None) if is_range_indexer(indexer, len(indexer)): - result = self.copy(deep=None) + result = self.copy(deep=False) if ignore_index: result.index = default_index(len(result)) From 5cd3c869bec3f71e20ac99b3ef9cf37f9849339e Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 10 Apr 2023 18:40:11 +0200 Subject: [PATCH 3/4] Fix --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e33d24681a422..580020185fa3f 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6771,7 +6771,7 @@ def sort_values( return self.copy(deep=None) if is_range_indexer(indexer, len(indexer)): - result = self.copy(deep=False) + result = self.copy(deep=not inplace) if ignore_index: result.index = default_index(len(result)) From 91d27b7398b395f9ff5b77989b3f97a757b03549 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 10 Apr 2023 21:21:40 +0200 Subject: [PATCH 4/4] Fix --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 580020185fa3f..c2edb7961cc41 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -6771,7 +6771,7 @@ def sort_values( return self.copy(deep=None) if is_range_indexer(indexer, len(indexer)): - result = self.copy(deep=not inplace) + result = self.copy(deep=(not inplace and not using_copy_on_write())) if ignore_index: result.index = default_index(len(result))