Skip to content

Commit 5f3c29e

Browse files
authored
BUG: df.setitem raising for ea bool df indexer (#50173)
1 parent 2f54a47 commit 5f3c29e

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ Interval
832832

833833
Indexing
834834
^^^^^^^^
835+
- Bug in :meth:`DataFrame.__setitem__` raising when indexer is a :class:`DataFrame` with ``boolean`` dtype (:issue:`47125`)
835836
- Bug in :meth:`DataFrame.reindex` filling with wrong values when indexing columns and index for ``uint`` dtypes (:issue:`48184`)
836837
- Bug in :meth:`DataFrame.loc` coercing dtypes when setting values with a list indexer (:issue:`49159`)
837838
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3947,7 +3947,7 @@ def _setitem_frame(self, key, value):
39473947
raise ValueError("Array conditional must be same shape as self")
39483948
key = self._constructor(key, **self._construct_axes_dict())
39493949

3950-
if key.size and not is_bool_dtype(key.values):
3950+
if key.size and not all(is_bool_dtype(dtype) for dtype in key.dtypes):
39513951
raise TypeError(
39523952
"Must pass DataFrame or 2-d ndarray with boolean values only"
39533953
)

pandas/tests/frame/indexing/test_setitem.py

+13
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,19 @@ def test_loc_setitem_all_false_boolean_two_blocks(self):
11251125
df.loc[indexer, ["b"]] = DataFrame({"b": [5, 6]}, index=[0, 1])
11261126
tm.assert_frame_equal(df, expected)
11271127

1128+
def test_setitem_ea_boolean_mask(self):
1129+
# GH#47125
1130+
df = DataFrame([[-1, 2], [3, -4]])
1131+
expected = DataFrame([[0, 2], [3, 0]])
1132+
boolean_indexer = DataFrame(
1133+
{
1134+
0: Series([True, False], dtype="boolean"),
1135+
1: Series([pd.NA, True], dtype="boolean"),
1136+
}
1137+
)
1138+
df[boolean_indexer] = 0
1139+
tm.assert_frame_equal(df, expected)
1140+
11281141

11291142
class TestDataFrameSetitemCopyViewSemantics:
11301143
def test_setitem_always_copy(self, float_frame):

0 commit comments

Comments
 (0)