Effective way to remove None from a numpy array

Is there an efficient way to remove Nones from numpy arrays and resize the array to its new size?

For example, how would you remove None from this frame without iterating through it in python. I can easily scroll through it, but worked on an api call, which could potentially be called many times.

a = np.array([1,45,23,23,1234,3432,-1232,-34,233,None]) 
+8
python numpy
source share
1 answer
 In [17]: a[a != np.array(None)] Out[17]: array([1, 45, 23, 23, 1234, 3432, -1232, -34, 233], dtype=object) 

The above works because a != np.array(None) is a boolean array that displays values ​​other than None:

 In [20]: a != np.array(None) Out[20]: array([ True, True, True, True, True, True, True, True, True, False], dtype=bool) 

Selecting array elements in this way is called boolean array indexing .

+16
source share

All Articles