From 753b4e60bd1bedd9ecd66b2b09ce7d4d6b5164ca Mon Sep 17 00:00:00 2001 From: phofl Date: Sat, 17 Apr 2021 16:41:42 +0200 Subject: [PATCH] Regression modifying obj with all false indexer --- pandas/core/indexing.py | 7 ++++++- pandas/tests/frame/indexing/test_setitem.py | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 82971e460a8a2..313ec5af4fe42 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -48,6 +48,7 @@ from pandas.core.construction import array as pd_array from pandas.core.indexers import ( check_array_indexer, + is_empty_indexer, is_exact_shape_match, is_list_like_indexer, length_of_indexer, @@ -1872,7 +1873,11 @@ def _setitem_single_column(self, loc: int, value, plane_indexer): # GH#6149 (null slice), GH#10408 (full bounds) if com.is_null_slice(pi) or com.is_full_slice(pi, len(self.obj)): ser = value - elif is_array_like(value) and is_exact_shape_match(ser, value): + elif ( + is_array_like(value) + and is_exact_shape_match(ser, value) + and not is_empty_indexer(pi, value) + ): if is_list_like(pi): ser = value[np.argsort(pi)] else: diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index 1d41426b93db6..9c153777c320a 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -888,6 +888,14 @@ def test_setitem_boolean_indexing(self): with pytest.raises(ValueError, match="Item wrong length"): df1[df1.index[:-1] > 2] = -1 + def test_loc_setitem_all_false_boolean_two_blocks(self): + # GH#40885 + df = DataFrame({"a": [1, 2], "b": [3, 4], "c": "a"}) + expected = df.copy() + indexer = Series([False, False], name="c") + df.loc[indexer, ["b"]] = DataFrame({"b": [5, 6]}, index=[0, 1]) + tm.assert_frame_equal(df, expected) + class TestDataFrameSetitemCopyViewSemantics: def test_setitem_always_copy(self, float_frame):