[Numpy-discussion] None as missing value

Travis Oliphant oliphant.travis at ieee.org
Mon Jul 3 01:55:13 EDT 2006


Keith Goodman wrote:
> I have a list x
>
>   
>>> x
>>>       
> [[1, None], [2, 3]]
>
> that I generate outside of numpy (with plain python). What is the best
> way to convert x into an array? This doesn't work
>
>   
>>> asarray(x)
>>>       
>
> array([[1, None],
>        [2, 3]], dtype=object)   <-- I'm hoping for something like dtype=float64
>
> Is there something better than None to represent missing values so
> that when I convert to numpy arrays (actually matrices) I'll be all
> set? (I could use -99, but that would be even more embarrassing than
> my python skills.)
>   

You can use a masked array specifically, or use nan's for missing values 
and just tell Python you want a floating-point array (because it finds 
the None object it's guessing incorrectly you want an "object" array.

asarray(x, dtype=float)

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


-Travis





More information about the NumPy-Discussion mailing list