Skip to content

Commit bdc08c9

Browse files
committed
Fixed handling of boolean indexing with 2-d ndarrays
Fixes #18582
1 parent c3c04e2 commit bdc08c9

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

pandas/core/frame.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2530,10 +2530,10 @@ def __setitem__(self, key, value):
25302530
if indexer is not None:
25312531
return self._setitem_slice(indexer, value)
25322532

2533-
if isinstance(key, (Series, np.ndarray, list, Index)):
2534-
self._setitem_array(key, value)
2535-
elif isinstance(key, DataFrame):
2533+
if isinstance(key, DataFrame) or getattr(key, 'ndim', None) == 2:
25362534
self._setitem_frame(key, value)
2535+
elif isinstance(key, (Series, np.ndarray, list, Index)):
2536+
self._setitem_array(key, value)
25372537
else:
25382538
# set column
25392539
self._set_item(key, value)
@@ -2566,8 +2566,17 @@ def _setitem_array(self, key, value):
25662566
def _setitem_frame(self, key, value):
25672567
# support boolean setting with DataFrame input, e.g.
25682568
# df[df > df2] = 0
2569+
if isinstance(key, np.ndarray):
2570+
if key.shape != self.shape:
2571+
raise ValueError(
2572+
'Array conditional must be same shape as self'
2573+
)
2574+
key = self._constructor(key, **self._construct_axes_dict())
2575+
25692576
if key.values.size and not is_bool_dtype(key.values):
2570-
raise TypeError('Must pass DataFrame with boolean values only')
2577+
raise TypeError(
2578+
'Must pass DataFrame or 2-d ndarray with boolean values only'
2579+
)
25712580

25722581
self._check_inplace_setting(value)
25732582
self._check_setitem_copy()

0 commit comments

Comments
 (0)