@@ -4717,24 +4717,41 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
47174717 applied as a function even if callable. Used in __setitem__.
47184718 """
47194719 inplace = validate_bool_kwarg (inplace , 'inplace' )
4720+ inplace = True if inplace else False
47204721
47214722 cond = com ._apply_if_callable (cond , self )
47224723
47234724 if isinstance (cond , NDFrame ):
47244725 cond , _ = cond .align (self , join = 'right' , broadcast_axis = 1 )
47254726 else :
47264727 if not hasattr (cond , 'shape' ):
4727- raise ValueError ('where requires an ndarray like object for '
4728- 'its condition' )
4728+ cond = np .asanyarray (cond )
47294729 if cond .shape != self .shape :
47304730 raise ValueError ('Array conditional must be same shape as '
47314731 'self' )
47324732 cond = self ._constructor (cond , ** self ._construct_axes_dict ())
47334733
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 ))
47364748 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
47384755
47394756 # try to align
47404757 try_quick = True
@@ -4883,26 +4900,20 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
48834900
48844901 Parameters
48854902 ----------
4886- cond : boolean %(klass)s, array or callable
4903+ cond : boolean %(klass)s, array-like, or callable
48874904 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).
48914907
48924908 .. versionadded:: 0.18.1
48934909
4894- A callable can be used as cond.
4895-
48964910 other : scalar, %(klass)s, or callable
48974911 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).
49014914
49024915 .. versionadded:: 0.18.1
49034916
4904- A callable can be used as other.
4905-
49064917 inplace : boolean, default False
49074918 Whether to perform the operation in place on the data
49084919 axis : alignment axis if needed, default None
0 commit comments