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