Numpy does treat float('nan') and float differently - convert to None
<p>I want to create a Numpy array form a normal array and convert <code>nan</code> values to <code>None</code> - but the success depends on weather the first value is a "normal" float, or a <code>float('nan')</code>.</p>

<p>Here is my code, starting with the initial array:</p>

<pre><code>print(a)
array('d', [3.2345, nan, 2.0, 3.2, 1.0, 3.0])
print(b)
array('d', [nan, nan, 2.0, 3.2, 1.0, 3.0])
</code></pre>

<p>Now I would like to swap all <code>nan</code> values to Python <code>None</code> via a vectorized function:</p>

<pre><code>def convert(x):
    if x != x:
        return None
    else:
        return x

convert_vec = numpy.vectorize(convert)
</code></pre>

<p>Simple, but leads to two different results:</p>

<pre><code>numpy.asarray(convert_vec(a))

array([[ 3.2345,  2.    ,  1.    ], [    nan,  3.2   ,  3.    ]])

numpy.asarray(convert_vec(b))
array([[None, 2.0, 1.0], [None, 3.2, 3.0]], dtype=object)
</code></pre>

<p>Why is this? Yes, I can see a small difference - the second one has <code>object</code> as <code>dtype</code>. But using <code>numpy.asarray(convert_vec(a), dtype=object)</code> fixed it - both have <code>object</code> as <code>dtype</code> - but it doesn't change the difference in results.</p>