Skip to content

Commit

Permalink
Fixed handling of boolean indexing with 2-d ndarrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dhirschfeld committed Dec 9, 2017
1 parent 27a64b2 commit 18e7a50
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions pandas/core/frame.py
Expand Up @@ -2530,10 +2530,10 @@ def __setitem__(self, key, value):
if indexer is not None:
return self._setitem_slice(indexer, value)

if isinstance(key, (Series, np.ndarray, list, Index)):
self._setitem_array(key, value)
elif isinstance(key, DataFrame):
if isinstance(key, DataFrame) or getattr(key, 'ndim', None) == 2:
self._setitem_frame(key, value)
elif isinstance(key, (Series, np.ndarray, list, Index)):
self._setitem_array(key, value)
else:
# set column
self._set_item(key, value)
Expand Down Expand Up @@ -2566,8 +2566,17 @@ def _setitem_array(self, key, value):
def _setitem_frame(self, key, value):
# support boolean setting with DataFrame input, e.g.
# df[df > df2] = 0
if isinstance(key, np.ndarray):
if key.shape != self.shape:
raise ValueError(
'Array conditional must be same shape as self'
)
key = self._constructor(key, **self._construct_axes_dict())

if key.values.size and not is_bool_dtype(key.values):
raise TypeError('Must pass DataFrame with boolean values only')
raise TypeError(
'Must pass DataFrame or 2-d ndarray with boolean values only'
)

self._check_inplace_setting(value)
self._check_setitem_copy()
Expand Down

0 comments on commit 18e7a50

Please sign in to comment.