From 4f0d461b6c8332b3bd14f7bda420d297cd1c1fb3 Mon Sep 17 00:00:00 2001 From: zitorelova Date: Thu, 28 Jan 2021 19:32:15 -0800 Subject: [PATCH 1/8] Fix sort_values bug --- pandas/core/frame.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 82e984d36b6a1..831daf340d0a8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5554,7 +5554,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: From c673fc85e9f3a18b88c93e455e0d2634ad07a68c Mon Sep 17 00:00:00 2001 From: zitorelova Date: Tue, 2 Feb 2021 22:48:53 +0000 Subject: [PATCH 2/8] Add test for sort_values index bug --- .../tests/frame/methods/test_sort_values.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index cd3286fa38056..6e4767f9c3f67 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -579,6 +579,25 @@ 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 + rand_low = 0 + rand_high = 100 + n_cols = 32 + n_rows = 4 + + random_state = np.random.RandomState(seed=42) + test_dict = { + int((i - n_cols / 2) % n_cols + 1): + random_state.randint(rand_low, rand_high, size=(n_rows)) + for i in range(n_cols) + } + df = pd.DataFrame(test_dict) + df = df.sort_values(df.index[0], axis=1, ignore_index=True) + + assert df.shape == (n_rows, n_cols) + assert list(df.columns) == list(range(n_cols)) + class TestDataFrameSortKey: # test key sorting (issue 27237) def test_sort_values_inplace_key(self, sort_by_key): From 373c64722cac8fe3f8e2c069f555ba9cda2bf26e Mon Sep 17 00:00:00 2001 From: zitorelova Date: Tue, 2 Feb 2021 22:58:17 +0000 Subject: [PATCH 3/8] Change usage of DataFrame to ensure consistency --- pandas/tests/frame/methods/test_sort_values.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index 6e4767f9c3f67..c7a3c1b8fafa3 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -592,7 +592,7 @@ def test_sort_values_reshaping(self): random_state.randint(rand_low, rand_high, size=(n_rows)) for i in range(n_cols) } - df = pd.DataFrame(test_dict) + df = DataFrame(test_dict) df = df.sort_values(df.index[0], axis=1, ignore_index=True) assert df.shape == (n_rows, n_cols) From 276478a94cf96b0cac100d69cfd957637fc6d9c4 Mon Sep 17 00:00:00 2001 From: zitorelova Date: Tue, 2 Feb 2021 23:11:01 +0000 Subject: [PATCH 4/8] Add whatsnew reshaping entry for 1.3 release --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 7fe0b53d7d2ff..f59fbfb4dc712 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -411,7 +411,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 (:issue:`39464`) Sparse ^^^^^^ From b6587fc548f51cfe26f44cb88b0b4dad8cd26b66 Mon Sep 17 00:00:00 2001 From: zitorelova Date: Tue, 2 Feb 2021 23:22:57 +0000 Subject: [PATCH 5/8] Refactor code based on black --- pandas/core/frame.py | 4 ++-- pandas/tests/frame/methods/test_sort_values.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 831daf340d0a8..9acbe07a77ff8 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5555,8 +5555,8 @@ def sort_values( # type: ignore[override] if ignore_index: new_data.set_axis( - self._get_block_manager_axis(axis), - ibase.default_index(len(indexer))) + 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 c7a3c1b8fafa3..589387fe959f1 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -588,8 +588,9 @@ def test_sort_values_reshaping(self): random_state = np.random.RandomState(seed=42) test_dict = { - int((i - n_cols / 2) % n_cols + 1): - random_state.randint(rand_low, rand_high, size=(n_rows)) + int((i - n_cols / 2) % n_cols + 1): random_state.randint( + rand_low, rand_high, size=(n_rows) + ) for i in range(n_cols) } df = DataFrame(test_dict) From 47f970a4b0c1be445b6dd05220da3351c1512b38 Mon Sep 17 00:00:00 2001 From: zitorelova Date: Wed, 3 Feb 2021 01:54:39 +0000 Subject: [PATCH 6/8] Fix input values for testing --- .../tests/frame/methods/test_sort_values.py | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index 1301f9e1c5378..053684ba08484 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -581,23 +581,11 @@ def test_sort_values_item_cache(self, using_array_manager): def test_sort_values_reshaping(self): # GH 39426 - rand_low = 0 - rand_high = 100 - n_cols = 32 - n_rows = 4 - - random_state = np.random.RandomState(seed=42) - test_dict = { - int((i - n_cols / 2) % n_cols + 1): random_state.randint( - rand_low, rand_high, size=(n_rows) - ) - for i in range(n_cols) - } - df = DataFrame(test_dict) - df = df.sort_values(df.index[0], axis=1, ignore_index=True) + values = list(range(21)) + expected = DataFrame([values], columns=values) + df = expected.sort_values(expected.index[0], axis=1, ignore_index=True) - assert df.shape == (n_rows, n_cols) - assert list(df.columns) == list(range(n_cols)) + tm.assert_frame_equal(df, expected) class TestDataFrameSortKey: # test key sorting (issue 27237) From fb363b2b9909e4782f8f70d6f97b7c799f798a90 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Tue, 2 Feb 2021 22:26:06 -0500 Subject: [PATCH 7/8] Update doc/source/whatsnew/v1.3.0.rst --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 1d1f573d65ab2..51b4a03e7d75b 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -412,7 +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 (:issue:`39464`) +- 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 From 8bcd8ee7bb724950c2924d2aeb99649e683d5f99 Mon Sep 17 00:00:00 2001 From: zitorelova Date: Wed, 3 Feb 2021 14:48:56 +0000 Subject: [PATCH 8/8] Double backtick code in doc/source/whatsnew/v1.3.0.rst --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 51b4a03e7d75b..b019c5b3d30e8 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -412,7 +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.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