Look at the arrays after you create them:
In [58]: a = np.array(['aug', False, False, False])
...:
In [59]: a
Out[59]: array(['aug', 'False', 'False', 'False'], dtype='<U5')
In [60]: a == 'aug'
Out[60]: array([ True, False, False, False])
In [61]: a == False
<ipython-input-61-f9ff25cfe387>:1: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
a == False
Out[61]: False
In [62]: a == 'False'
Out[62]: array([False, True, True, True])
It's a string dtype array. Testing for matching strings works. Testing for a nonstring value is wrong.
Same for nan
:
In [64]: a = np.array(['aug', np.nan, np.nan, np.nan])
In [65]: a
Out[65]: array(['aug', 'nan', 'nan', 'nan'], dtype='<U3')
In [66]: a == 'nan'
Out[66]: array([False, True, True, True])
If you must mix types - string, boolean, float, you can specify object
dtype. That makes the array more list-like.
In [67]: a = np.array(['aug', False, False, False], object)
In [68]: a
Out[68]: array(['aug', False, False, False], dtype=object)
In [69]: a == 'aug'
Out[69]: array([ True, False, False, False])
In [70]: a == False
Out[70]: array([False, True, True, True])
In [71]: a == True
Out[71]: array([False, False, False, False])
But it doesn't help with np.nan
. nan
is a special kind of float that isn't equal to anything, not even itself.
In [72]: a = np.array(['aug', np.nan, np.nan, np.nan], object)
In [73]: a
Out[73]: array(['aug', nan, nan, nan], dtype=object)
In [74]: a=='aug'
Out[74]: array([ True, False, False, False])
In [75]: a == np.nan
Out[75]: array([False, False, False, False])
isnan
is the correct way to test for nan
, but it only works with float dtype arrays:
In [76]: np.isnan(a)
Traceback (most recent call last):
File "<ipython-input-76-da86cb21b8a4>", line 1, in <module>
np.isnan(a)
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''