why don't I get index returns from list comprehension checking truth values?
<p>So I have a numpy array with numeric values, floats to be exact and NaNs:</p>

<pre><code>In [1]: type(myarray)
Out[1]: numpy.ndarray
</code></pre>

<p>now I want to check where there are NaNs in this array so I try:</p>

<pre><code>nan_idx = [idx for idx, tf in enumerate(myarray) if not bool(tf)]
</code></pre>

<p>However this returns an empty list, but when I try:</p>

<pre><code>np.where(pd.isnull(mayarray))[0]
</code></pre>

<p>I do get as output the index of each NaN</p>

<p>Why doesn't the first attempt produce any results? </p>

<p>The reason your first snippet doesn't work is that <code>bool(float)</code> does not check for NaNs:</p>

<pre><code>&gt;&gt;&gt; float('NaN')
nan
&gt;&gt;&gt; bool(float('NaN'))
True
&gt;&gt;&gt; not bool(float('NaN'))
False
</code></pre>

<p>To check for NaNs, you could use <code>numpy.isnan</code> or the fact that a NaN doesn't compare equal to itself:</p>

<pre><code>&gt;&gt;&gt; numpy.isnan(float('NaN'))
True
&gt;&gt;&gt; float('NaN') != float('NaN')
True
</code></pre>

python numpy