Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert python list with None values to numpy array with nan values

Tags:

python

numpy

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?

like image 376
Akavall Avatar asked Oct 18 '13 18:10

Akavall


People also ask

How do I initialize a NumPy array with NaN?

To create an array with nan values we have to use the numpy. empty() and fill() function. It returns an array with the same shape and type as a given array.

How do I replace 0 with NaN NumPy?

nan_to_num() in Python. numpy. nan_to_num() function is used when we want to replace nan(Not A Number) with zero and inf with finite numbers in an array. It returns (positive) infinity with a very large number and negative infinity with a very small (or negative) number.

How does NumPy array deal with NaN values?

The most common way to do so is by using the . fillna() method. This method requires you to specify a value to replace the NaNs with.


2 Answers

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]) 
like image 126
Jaime Avatar answered Oct 13 '22 09:10

Jaime


What about

my_array = np.array(map(lambda x: numpy.nan if x==None else x, my_list)) 
like image 39
Udo Klein Avatar answered Oct 13 '22 10:10

Udo Klein