@@ -4724,17 +4724,33 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
47244724 cond , _ = cond .align (self , join = 'right' , broadcast_axis = 1 )
47254725 else :
47264726 if not hasattr (cond , 'shape' ):
4727- raise ValueError ('where requires an ndarray like object for '
4728- 'its condition' )
4727+ cond = np .asanyarray (cond )
47294728 if cond .shape != self .shape :
47304729 raise ValueError ('Array conditional must be same shape as '
47314730 'self' )
47324731 cond = self ._constructor (cond , ** self ._construct_axes_dict ())
47334732
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 ))
47364747 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
47384754
47394755 # try to align
47404756 try_quick = True
@@ -4883,26 +4899,20 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
48834899
48844900 Parameters
48854901 ----------
4886- cond : boolean %(klass)s, array or callable
4902+ cond : boolean %(klass)s, array-like, or callable
48874903 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).
48914906
48924907 .. versionadded:: 0.18.1
48934908
4894- A callable can be used as cond.
4895-
48964909 other : scalar, %(klass)s, or callable
48974910 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).
49014913
49024914 .. versionadded:: 0.18.1
49034915
4904- A callable can be used as other.
4905-
49064916 inplace : boolean, default False
49074917 Whether to perform the operation in place on the data
49084918 axis : alignment axis if needed, default None
0 commit comments