47

I am trying to convert a list that contains numeric values and None values to numpy.array, such that None is replaces with numpy.nan.

For example:

my_list = [3,5,6,None,6,None]

# My desired result: 
my_array = numpy.array([3,5,6,np.nan,6,np.nan]) 

Naive approach fails:

>>> my_list
[3, 5, 6, None, 6, None]
>>> np.array(my_list)
array([3, 5, 6, None, 6, None], dtype=object) # very limited 
>>> _ * 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

>>> my_array # normal array can handle these operations
array([  3.,   5.,   6.,  nan,   6.,  nan])
>>> my_array * 2
array([  6.,  10.,  12.,  nan,  12.,  nan])

What is the best way to solve this problem?

2 Answers 2

67

You simply have to explicitly declare the data type:

>>> my_list = [3, 5, 6, None, 6, None]
>>> np.array(my_list, dtype=float)
array([  3.,   5.,   6.,  nan,   6.,  nan])
Sign up to request clarification or add additional context in comments.

8 Comments

I had a feeling that I was missing something simple...Thank You
The problem with this approach is that it only works for float for which nan is defined and not for dtype int.
Justy a heads-up that np.float is deprecated. use dtype=float instead
Interestingly this turns them to plain python float nan, not np.nan. I wonder why as it may seem a little irregular, even though both types of nan qualify the same under np.isnan() . This is so even when using dtype=np.float64 which is explicitly a numpy data type. This conversion is not even explicitly documented for np.array()
DeprecationWarning: np.float is a deprecated alias for the builtin float
|
7

What about

my_array = np.array(map(lambda x: numpy.nan if x==None else x, my_list))

2 Comments

I'd say x is None as == might be more quirky
What worked for me was my_array = np.array(list(map(lambda x: np.nan if x is None else x, my_list)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.