Skip to content

Commit fc4849a

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

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
@@ -2532,10 +2532,10 @@ def __setitem__(self, key, value):
25322532
if indexer is not None:
25332533
return self._setitem_slice(indexer, value)
25342534

2535-
if isinstance(key, (Series, np.ndarray, list, Index)):
2536-
self._setitem_array(key, value)
2537-
elif isinstance(key, DataFrame):
2535+
if isinstance(key, DataFrame) or getattr(key, 'ndim', None) == 2:
25382536
self._setitem_frame(key, value)
2537+
elif isinstance(key, (Series, np.ndarray, list, Index)):
2538+
self._setitem_array(key, value)
25392539
else:
25402540
# set column
25412541
self._set_item(key, value)
@@ -2568,8 +2568,17 @@ def _setitem_array(self, key, value):
25682568
def _setitem_frame(self, key, value):
25692569
# support boolean setting with DataFrame input, e.g.
25702570
# df[df > df2] = 0
2571+
if isinstance(key, np.ndarray):
2572+
if key.shape != self.shape:
2573+
raise ValueError(
2574+
'Array conditional must be same shape as self'
2575+
)
2576+
key = self._constructor(key, **self._construct_axes_dict())
2577+
25712578
if key.values.size and not is_bool_dtype(key.values):
2572-
raise TypeError('Must pass DataFrame with boolean values only')
2579+
raise TypeError(
2580+
'Must pass DataFrame or 2-d ndarray with boolean values only'
2581+
)
25732582

25742583
self._check_inplace_setting(value)
25752584
self._check_setitem_copy()

0 commit comments

Comments
 (0)