Skip to content

BUG: df.setitem raising for ea bool df indexer #50173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ Interval

Indexing
^^^^^^^^
- Bug in :meth:`DataFrame.__setitem__` raising when indexer is a :class:`DataFrame` with ``boolean`` dtype (:issue:`47125`)
- Bug in :meth:`DataFrame.reindex` filling with wrong values when indexing columns and index for ``uint`` dtypes (:issue:`48184`)
- Bug in :meth:`DataFrame.loc` coercing dtypes when setting values with a list indexer (:issue:`49159`)
- Bug in :meth:`DataFrame.loc` raising ``ValueError`` with ``bool`` indexer and :class:`MultiIndex` (:issue:`47687`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4087,7 +4087,7 @@ def _setitem_frame(self, key, value):
raise ValueError("Array conditional must be same shape as self")
key = self._constructor(key, **self._construct_axes_dict())

if key.size and not is_bool_dtype(key.values):
if key.size and not all(is_bool_dtype(dtype) for dtype in key.dtypes):
raise TypeError(
"Must pass DataFrame or 2-d ndarray with boolean values only"
)
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,19 @@ def test_loc_setitem_all_false_boolean_two_blocks(self):
df.loc[indexer, ["b"]] = DataFrame({"b": [5, 6]}, index=[0, 1])
tm.assert_frame_equal(df, expected)

def test_setitem_ea_boolean_mask(self):
# GH#47125
df = DataFrame([[-1, 2], [3, -4]])
expected = DataFrame([[0, 2], [3, 0]])
boolean_indexer = DataFrame(
{
0: Series([True, False], dtype="boolean"),
1: Series([pd.NA, True], dtype="boolean"),
}
)
df[boolean_indexer] = 0
tm.assert_frame_equal(df, expected)


class TestDataFrameSetitemCopyViewSemantics:
def test_setitem_always_copy(self, float_frame):
Expand Down