From 110d8c0ef2b5dfb7be7698063f8a082008fe35e9 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] Backport PR #48711: REGR: Regression in DataFrame.loc when setting df with all True indexer --- 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 4a7eaf20a8745..4717c6fcf5ea1 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 a9919f8f2e290..85f3499b4375a 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1982,7 +1982,7 @@ def _setitem_single_column(self, loc: int, value, plane_indexer): 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):