From 4078889a44dec8093f2ddc0233faf2e745648cce Mon Sep 17 00:00:00 2001 From: Noy Hanan Date: Thu, 4 May 2023 17:32:20 +0300 Subject: [PATCH 1/3] Added test for sort_index parameter multiindex 'sort_remaining' = False --- pandas/tests/frame/methods/test_sort_index.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index af55bc424d396..fe6588c8c400b 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -914,3 +914,22 @@ def test_sort_index_na_position(self): expected = df.copy() result = df.sort_index(level=[0, 1], na_position="last") tm.assert_frame_equal(result, expected) + + @pytest.mark.parametrize("ascending", [True, False]) + def test_sort_index_multiindex_sort_remaining(self, ascending): + # GH #24247, testing sort_remaining=false consistent behavior for ascending = True, ascending = False + df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50]}, + index=pd.MultiIndex.from_tuples([('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y'), ('c', 'x')])) + df_sorted = df.sort_index(level=1, sort_remaining=False, ascending=ascending) + + if ascending: + df_expected = pd.DataFrame({'A': [1, 3, 5, 2, 4], 'B': [10, 30, 50, 20, 40]}, + index=pd.MultiIndex.from_tuples( + [('a', 'x'), ('b', 'x'), ('c', 'x'), ('a', 'y'), ('b', 'y')])) + else: + df_expected = pd.DataFrame({'A': [2, 4, 1, 3, 5], 'B': [20, 40, 10, 30, 50]}, + index=pd.MultiIndex.from_tuples( + [('a', 'y'), ('b', 'y'), ('a', 'x'), ('b', 'x'), ('c', 'x')])) + + # Assert that the resulting DataFrame is sorted correctly + tm.assert_frame_equal(df_sorted, df_expected) From 3eacec15372b5982d0e4ff1820ca3eaad566ff07 Mon Sep 17 00:00:00 2001 From: Noy Hanan Date: Thu, 4 May 2023 20:23:21 +0300 Subject: [PATCH 2/3] changes pd.MultiIndex to MultiIndex --- pandas/tests/frame/methods/test_sort_index.py | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index fe6588c8c400b..f7465644b708a 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -917,19 +917,31 @@ def test_sort_index_na_position(self): @pytest.mark.parametrize("ascending", [True, False]) def test_sort_index_multiindex_sort_remaining(self, ascending): - # GH #24247, testing sort_remaining=false consistent behavior for ascending = True, ascending = False - df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50]}, - index=pd.MultiIndex.from_tuples([('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y'), ('c', 'x')])) + # GH #24247, testing multiindex sort_remaining=false consistent behavior + # for ascending = True, ascending = False + df = DataFrame( + {"A": [1, 2, 3, 4, 5], "B": [10, 20, 30, 40, 50]}, + index=MultiIndex.from_tuples( + [("a", "x"), ("a", "y"), ("b", "x"), ("b", "y"), ("c", "x")] + ), + ) + df_sorted = df.sort_index(level=1, sort_remaining=False, ascending=ascending) if ascending: - df_expected = pd.DataFrame({'A': [1, 3, 5, 2, 4], 'B': [10, 30, 50, 20, 40]}, - index=pd.MultiIndex.from_tuples( - [('a', 'x'), ('b', 'x'), ('c', 'x'), ('a', 'y'), ('b', 'y')])) + df_expected = DataFrame( + {"A": [1, 3, 5, 2, 4], "B": [10, 30, 50, 20, 40]}, + index=MultiIndex.from_tuples( + [("a", "x"), ("b", "x"), ("c", "x"), ("a", "y"), ("b", "y")] + ), + ) else: - df_expected = pd.DataFrame({'A': [2, 4, 1, 3, 5], 'B': [20, 40, 10, 30, 50]}, - index=pd.MultiIndex.from_tuples( - [('a', 'y'), ('b', 'y'), ('a', 'x'), ('b', 'x'), ('c', 'x')])) + df_expected = DataFrame( + {"A": [2, 4, 1, 3, 5], "B": [20, 40, 10, 30, 50]}, + index=MultiIndex.from_tuples( + [("a", "y"), ("b", "y"), ("a", "x"), ("b", "x"), ("c", "x")] + ), + ) # Assert that the resulting DataFrame is sorted correctly tm.assert_frame_equal(df_sorted, df_expected) From 1cd19c89cb8f823091d8cea6a234be8780f5980e Mon Sep 17 00:00:00 2001 From: Noy Hanan Date: Sat, 6 May 2023 09:12:27 +0300 Subject: [PATCH 3/3] remove notes and changes variable names --- pandas/tests/frame/methods/test_sort_index.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py index f7465644b708a..d7de369703ae9 100644 --- a/pandas/tests/frame/methods/test_sort_index.py +++ b/pandas/tests/frame/methods/test_sort_index.py @@ -917,8 +917,7 @@ def test_sort_index_na_position(self): @pytest.mark.parametrize("ascending", [True, False]) def test_sort_index_multiindex_sort_remaining(self, ascending): - # GH #24247, testing multiindex sort_remaining=false consistent behavior - # for ascending = True, ascending = False + # GH #24247 df = DataFrame( {"A": [1, 2, 3, 4, 5], "B": [10, 20, 30, 40, 50]}, index=MultiIndex.from_tuples( @@ -926,22 +925,21 @@ def test_sort_index_multiindex_sort_remaining(self, ascending): ), ) - df_sorted = df.sort_index(level=1, sort_remaining=False, ascending=ascending) + result = df.sort_index(level=1, sort_remaining=False, ascending=ascending) if ascending: - df_expected = DataFrame( + expected = DataFrame( {"A": [1, 3, 5, 2, 4], "B": [10, 30, 50, 20, 40]}, index=MultiIndex.from_tuples( [("a", "x"), ("b", "x"), ("c", "x"), ("a", "y"), ("b", "y")] ), ) else: - df_expected = DataFrame( + expected = DataFrame( {"A": [2, 4, 1, 3, 5], "B": [20, 40, 10, 30, 50]}, index=MultiIndex.from_tuples( [("a", "y"), ("b", "y"), ("a", "x"), ("b", "x"), ("c", "x")] ), ) - # Assert that the resulting DataFrame is sorted correctly - tm.assert_frame_equal(df_sorted, df_expected) + tm.assert_frame_equal(result, expected)