From a74606896c4c8ae7c31bea7e857c3fc0823f30f6 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Thu, 22 Sep 2022 10:50:13 -0700 Subject: [PATCH] REGR: Regression in DataFrame.loc when setting df with all True indexer (#48711) --- doc/source/whatsnew/v1.5.1.rst | 1 + pandas/core/indexing.py | 2 +- pandas/tests/frame/indexing/test_indexing.py | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index 0c00e26b40a3d..c8a3c95888a89 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -14,6 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ +- Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`) - Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`) - Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`) - diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 60b687167a7a3..bca8b3d459245 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1986,7 +1986,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer) -> None: and self.obj.shape[0] == value.shape[0] and not is_empty_indexer(pi) ): - if is_list_like(pi): + if is_list_like(pi) and not is_bool_dtype(pi): value = value[np.argsort(pi)] else: # in case of slice diff --git a/pandas/tests/frame/indexing/test_indexing.py b/pandas/tests/frame/indexing/test_indexing.py index 7f84d8a367eac..3062dff27d537 100644 --- a/pandas/tests/frame/indexing/test_indexing.py +++ b/pandas/tests/frame/indexing/test_indexing.py @@ -1405,6 +1405,15 @@ def test_loc_named_tuple_for_midx(self): ) tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize("col", [{}, {"name": "a"}]) + def test_loc_setitem_reordering_with_all_true_indexer(self, col): + # GH#48701 + n = 17 + df = DataFrame({**col, "x": range(n), "y": range(n)}) + expected = df.copy() + df.loc[n * [True], ["x", "y"]] = df[["x", "y"]] + tm.assert_frame_equal(df, expected) + class TestDataFrameIndexingUInt64: def test_setitem(self, uint64_frame):