41

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?

Akavall
  • 82,592
  • 51
  • 207
  • 251

2 Answers2

57

You simply have to explicitly declare the data type:

>>> my_list = [3, 5, 6, None, 6, None]
>>> np.array(my_list, dtype=np.float)
array([  3.,   5.,   6.,  nan,   6.,  nan])
Jaime
  • 65,696
  • 17
  • 124
  • 159
7

What about

my_array = np.array(map(lambda x: numpy.nan if x==None else x, my_list))
Udo Klein
  • 6,784
  • 1
  • 36
  • 61
  • 5
    I'd say `x is None` as == might be more [quirky](https://stackoverflow.com/questions/14247373/python-none-comparison-should-i-use-is-or) – alexey Oct 19 '17 at 04:40
  • What worked for me was `my_array = np.array(list(map(lambda x: np.nan if x is None else x, my_list)))` – Esteban Jan 18 '23 at 18:46