28
28
29
29
from pandas ._libs import lib
30
30
from pandas ._libs .lib import is_range_indexer
31
- from pandas ._libs .missing import NA
32
31
from pandas ._libs .tslibs import (
33
32
Period ,
34
33
Timestamp ,
114
113
is_bool_dtype ,
115
114
is_dict_like ,
116
115
is_extension_array_dtype ,
116
+ is_float_dtype ,
117
117
is_list_like ,
118
118
is_number ,
119
119
is_numeric_dtype ,
@@ -9713,6 +9713,7 @@ def _where(
9713
9713
if axis is not None :
9714
9714
axis = self ._get_axis_number (axis )
9715
9715
9716
+ has_nan : bool = False
9716
9717
# align the cond to same shape as myself
9717
9718
cond = common .apply_if_callable (cond , self )
9718
9719
if isinstance (cond , NDFrame ):
@@ -9728,9 +9729,13 @@ def _where(
9728
9729
else :
9729
9730
if not hasattr (cond , "shape" ):
9730
9731
cond = np .asanyarray (cond )
9732
+ if is_float_dtype (cond ) and np .isnan (cond ).any ():
9733
+ has_nan = True
9731
9734
if cond .shape != self .shape :
9732
9735
raise ValueError ("Array conditional must be same shape as self" )
9733
9736
cond = self ._constructor (cond , ** self ._construct_axes_dict (), copy = False )
9737
+ if has_nan :
9738
+ cond = cond .replace ({0.0 : False , 1.0 : True })
9734
9739
cond = cond .fillna (True )
9735
9740
9736
9741
# make sure we are boolean
@@ -10113,13 +10118,13 @@ def mask(
10113
10118
# see gh-21891
10114
10119
if not hasattr (cond , "__invert__" ):
10115
10120
cond = np .array (cond )
10121
+ if is_float_dtype (cond ) and np .isnan (cond ).any ():
10122
+ cond = cond .astype (bool )
10116
10123
10117
10124
if isinstance (cond , np .ndarray ):
10118
- if all (x is NA or lib .is_bool (x ) or x is np .nan for x in cond .flatten ()):
10119
- if not cond .flags .writeable :
10120
- cond .setflags (write = True )
10121
- cond [isna (cond )] = False
10122
- cond = cond .astype (bool )
10125
+ if not cond .flags .writeable :
10126
+ cond .setflags (write = True )
10127
+ cond [isna (cond )] = False
10123
10128
10124
10129
return self ._where (
10125
10130
~ cond ,
0 commit comments