Skip to content

Commit

Permalink
Regression modifying obj with all false indexer (#40999)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Apr 19, 2021
1 parent bcecb38 commit 69f225a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1882,7 +1883,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:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,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):
Expand Down

0 comments on commit 69f225a

Please sign in to comment.