@@ -4724,17 +4724,33 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
4724
4724
cond , _ = cond .align (self , join = 'right' , broadcast_axis = 1 )
4725
4725
else :
4726
4726
if not hasattr (cond , 'shape' ):
4727
- raise ValueError ('where requires an ndarray like object for '
4728
- 'its condition' )
4727
+ cond = np .asanyarray (cond )
4729
4728
if cond .shape != self .shape :
4730
4729
raise ValueError ('Array conditional must be same shape as '
4731
4730
'self' )
4732
4731
cond = self ._constructor (cond , ** self ._construct_axes_dict ())
4733
4732
4734
- if inplace :
4735
- cond = - (cond .fillna (True ).astype (bool ))
4733
+ # If 'inplace' is True, we want to fill with True
4734
+ # before inverting. If 'inplace' is False, we will
4735
+ # fill with False and do nothing else.
4736
+ #
4737
+ # Conveniently, 'inplace' matches the boolean with
4738
+ # which we want to fill.
4739
+ cond = cond .fillna (bool (inplace ))
4740
+
4741
+ msg = "Boolean array expected for the condition, not {dtype}"
4742
+
4743
+ if not isinstance (cond , pd .DataFrame ):
4744
+ # This is a single-dimensional object.
4745
+ if not is_bool_dtype (cond ):
4746
+ raise ValueError (msg .format (dtype = cond .dtype ))
4736
4747
else :
4737
- cond = cond .fillna (False ).astype (bool )
4748
+ for dt in cond .dtypes :
4749
+ if not is_bool_dtype (dt ):
4750
+ raise ValueError (msg .format (dtype = dt ))
4751
+
4752
+ cond = cond .astype (bool )
4753
+ cond = - cond if inplace else cond
4738
4754
4739
4755
# try to align
4740
4756
try_quick = True
@@ -4883,26 +4899,20 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
4883
4899
4884
4900
Parameters
4885
4901
----------
4886
- cond : boolean %(klass)s, array or callable
4902
+ cond : boolean %(klass)s, array-like, or callable
4887
4903
If cond is callable, it is computed on the %(klass)s and
4888
- should return boolean %(klass)s or array.
4889
- The callable must not change input %(klass)s
4890
- (though pandas doesn't check it).
4904
+ should return boolean %(klass)s or array. The callable must
4905
+ not change input %(klass)s (though pandas doesn't check it).
4891
4906
4892
4907
.. versionadded:: 0.18.1
4893
4908
4894
- A callable can be used as cond.
4895
-
4896
4909
other : scalar, %(klass)s, or callable
4897
4910
If other is callable, it is computed on the %(klass)s and
4898
- should return scalar or %(klass)s.
4899
- The callable must not change input %(klass)s
4900
- (though pandas doesn't check it).
4911
+ should return scalar or %(klass)s. The callable must not
4912
+ change input %(klass)s (though pandas doesn't check it).
4901
4913
4902
4914
.. versionadded:: 0.18.1
4903
4915
4904
- A callable can be used as other.
4905
-
4906
4916
inplace : boolean, default False
4907
4917
Whether to perform the operation in place on the data
4908
4918
axis : alignment axis if needed, default None
0 commit comments