From f55c96879c3441b0aa9409da28b96b87ce839a57 Mon Sep 17 00:00:00 2001 From: Saehui Hwang Date: Fri, 15 Oct 2021 00:23:35 -0700 Subject: [PATCH 1/6] BUG: sort_index did not respect ignore_index when not sorting --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/core/generic.py | 9 ++++++++- pandas/tests/test_sorting.py | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 243bcf6900d2e..b10ebd350459a 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -430,6 +430,7 @@ Indexing - Bug in :meth:`DataFrame.drop` where the error message did not show missing labels with commas when raising ``KeyError`` (:issue:`42881`) - Bug in :meth:`DataFrame.query` where method calls in query strings led to errors when the ``numexpr`` package was installed. (:issue:`22435`) - Bug in :meth:`DataFrame.nlargest` and :meth:`Series.nlargest` where sorted result did not count indexes containing ``np.nan`` (:issue:`28984`) +- Bug in :meth:`DataFrame.sort_index` where `ignore_index=True` was not being respected when the passed dataframe was already sorted (:issue:`43591`) Missing diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 26c0b7426727c..5d5765977c49b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -4590,10 +4590,17 @@ def sort_index( ) if indexer is None: + if inplace: + result = self + else: + result = self.copy() + + if ignore_index: + result.index = default_index(len(self)) if inplace: return else: - return self.copy() + return result baxis = self._get_block_manager_axis(axis) new_data = self._mgr.take(indexer, axis=baxis, verify=False) diff --git a/pandas/tests/test_sorting.py b/pandas/tests/test_sorting.py index a49b7c2b7f86e..17c1703831e3f 100644 --- a/pandas/tests/test_sorting.py +++ b/pandas/tests/test_sorting.py @@ -478,3 +478,12 @@ def test_mixed_str_nan(): result = safe_sort(values) expected = np.array([np.nan, "a", "b", "b"], dtype=object) tm.assert_numpy_array_equal(result, expected) + +def test_respect_ignore_index(): + # GH 43591 + df = DataFrame({'a': [1, 2, 3]}) + df.index = [4, 2, 0] + result = df.sort_index(ascending=False, ignore_index=True) + expected = DataFrame({'a': [1, 2, 3]}) + expected.index = [0, 1, 2] + tm.assert_frame_equal(result, expected) \ No newline at end of file From e56f8fb6b8d1a8db1f18f5a52a1337824a2277e6 Mon Sep 17 00:00:00 2001 From: Saehui Hwang Date: Fri, 15 Oct 2021 00:26:24 -0700 Subject: [PATCH 2/6] BUG: sort_index did not respect ignore_index when not sorting --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index b10ebd350459a..780e6dddb47f0 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -430,7 +430,7 @@ Indexing - Bug in :meth:`DataFrame.drop` where the error message did not show missing labels with commas when raising ``KeyError`` (:issue:`42881`) - Bug in :meth:`DataFrame.query` where method calls in query strings led to errors when the ``numexpr`` package was installed. (:issue:`22435`) - Bug in :meth:`DataFrame.nlargest` and :meth:`Series.nlargest` where sorted result did not count indexes containing ``np.nan`` (:issue:`28984`) -- Bug in :meth:`DataFrame.sort_index` where `ignore_index=True` was not being respected when the passed dataframe was already sorted (:issue:`43591`) +- Bug in :meth:`DataFrame.sort_index` where `ignore_index=True` was not being respected when passed dataframe was already sorted (:issue:`43591`) Missing From 282ef5148fa2242d667be777f117e2273908e4c6 Mon Sep 17 00:00:00 2001 From: Saehui Hwang Date: Fri, 15 Oct 2021 00:37:07 -0700 Subject: [PATCH 3/6] BUG: sort_index did not respect ignore_index when not sorting --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 780e6dddb47f0..b10ebd350459a 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -430,7 +430,7 @@ Indexing - Bug in :meth:`DataFrame.drop` where the error message did not show missing labels with commas when raising ``KeyError`` (:issue:`42881`) - Bug in :meth:`DataFrame.query` where method calls in query strings led to errors when the ``numexpr`` package was installed. (:issue:`22435`) - Bug in :meth:`DataFrame.nlargest` and :meth:`Series.nlargest` where sorted result did not count indexes containing ``np.nan`` (:issue:`28984`) -- Bug in :meth:`DataFrame.sort_index` where `ignore_index=True` was not being respected when passed dataframe was already sorted (:issue:`43591`) +- Bug in :meth:`DataFrame.sort_index` where `ignore_index=True` was not being respected when the passed dataframe was already sorted (:issue:`43591`) Missing From 2c5402ea64729473367e8c19cee1949a9cf9ce5d Mon Sep 17 00:00:00 2001 From: Saehui Hwang Date: Fri, 15 Oct 2021 00:38:56 -0700 Subject: [PATCH 4/6] BUG: sort_index did not respect ignore_index when not sorting --- pandas/tests/test_sorting.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/tests/test_sorting.py b/pandas/tests/test_sorting.py index 17c1703831e3f..c75815856c9ff 100644 --- a/pandas/tests/test_sorting.py +++ b/pandas/tests/test_sorting.py @@ -479,11 +479,12 @@ def test_mixed_str_nan(): expected = np.array([np.nan, "a", "b", "b"], dtype=object) tm.assert_numpy_array_equal(result, expected) + def test_respect_ignore_index(): # GH 43591 - df = DataFrame({'a': [1, 2, 3]}) + df = DataFrame({"a": [1, 2, 3]}) df.index = [4, 2, 0] result = df.sort_index(ascending=False, ignore_index=True) - expected = DataFrame({'a': [1, 2, 3]}) + expected = DataFrame({"a": [1, 2, 3]}) expected.index = [0, 1, 2] - tm.assert_frame_equal(result, expected) \ No newline at end of file + tm.assert_frame_equal(result, expected) From 837427bd96fee168ff77e76e8e730a50adbc6c10 Mon Sep 17 00:00:00 2001 From: Saehui Hwang Date: Sat, 16 Oct 2021 19:56:06 -0700 Subject: [PATCH 5/6] moved test to frame test directory --- pandas/tests/frame/methods/test_sort_index.py | 8 ++++++++ pandas/tests/test_sorting.py | 10 ---------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index c1141f705acbc..c78a6d015cc16 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -9,6 +9,7 @@ Index, IntervalIndex, MultiIndex, + RangeIndex, Series, Timestamp, ) @@ -418,6 +419,13 @@ def test_sort_index_ignore_index( tm.assert_frame_equal(result_df, expected_df) tm.assert_frame_equal(df, DataFrame(original_dict, index=original_index)) + def test_respect_ignore_index(self): + # GH 43591 + df = DataFrame({"a": [1, 2, 3]}, index=RangeIndex(4, -1, -2)) + result = df.sort_index(ascending=False, ignore_index=True) + expected = DataFrame({"a": [1, 2, 3]}) + tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize("inplace", [True, False]) @pytest.mark.parametrize( "original_dict, sorted_dict, ascending, ignore_index, output_index", diff --git a/pandas/tests/test_sorting.py b/pandas/tests/test_sorting.py index c75815856c9ff..a49b7c2b7f86e 100644 --- a/pandas/tests/test_sorting.py +++ b/pandas/tests/test_sorting.py @@ -478,13 +478,3 @@ def test_mixed_str_nan(): result = safe_sort(values) expected = np.array([np.nan, "a", "b", "b"], dtype=object) tm.assert_numpy_array_equal(result, expected) - - -def test_respect_ignore_index(): - # GH 43591 - df = DataFrame({"a": [1, 2, 3]}) - df.index = [4, 2, 0] - result = df.sort_index(ascending=False, ignore_index=True) - expected = DataFrame({"a": [1, 2, 3]}) - expected.index = [0, 1, 2] - tm.assert_frame_equal(result, expected) From 7523b1b84cc30898a8d8b3acff34033682e54897 Mon Sep 17 00:00:00 2001 From: Saehui Hwang Date: Mon, 18 Oct 2021 21:39:59 -0700 Subject: [PATCH 6/6] parameterized over inplace and ignore_index --- pandas/tests/frame/methods/test_sort_index.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index c78a6d015cc16..71822628473f4 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -419,11 +419,22 @@ def test_sort_index_ignore_index( tm.assert_frame_equal(result_df, expected_df) tm.assert_frame_equal(df, DataFrame(original_dict, index=original_index)) - def test_respect_ignore_index(self): + @pytest.mark.parametrize("inplace", [True, False]) + @pytest.mark.parametrize("ignore_index", [True, False]) + def test_respect_ignore_index(self, inplace, ignore_index): # GH 43591 df = DataFrame({"a": [1, 2, 3]}, index=RangeIndex(4, -1, -2)) - result = df.sort_index(ascending=False, ignore_index=True) - expected = DataFrame({"a": [1, 2, 3]}) + result = df.sort_index( + ascending=False, ignore_index=ignore_index, inplace=inplace + ) + + if inplace: + result = df + if ignore_index: + expected = DataFrame({"a": [1, 2, 3]}) + else: + expected = DataFrame({"a": [1, 2, 3]}, index=RangeIndex(4, -1, -2)) + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("inplace", [True, False])