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?
asked Oct 18, 2013 at 18:03
Akavall
86.9k58 gold badges215 silver badges261 bronze badges
2 Answers 2
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])
Luís de Sousa
7,04911 gold badges56 silver badges94 bronze badges
answered Oct 18, 2013 at 18:10
Jaime
67.7k19 gold badges128 silver badges164 bronze badges
Sign up to request clarification or add additional context in comments.
8 Comments
Akavall
I had a feeling that I was missing something simple...Thank You
Andrzej Pronobis
The problem with this approach is that it only works for float for which nan is defined and not for dtype int.
Babak
Justy a heads-up that
np.float is deprecated. use dtype=float insteadmatanox
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() Gideon Kogan
DeprecationWarning:
np.float is a deprecated alias for the builtin float |
What about
my_array = np.array(map(lambda x: numpy.nan if x==None else x, my_list))
answered Oct 18, 2013 at 18:07
Udo Klein
6,9401 gold badge40 silver badges63 bronze badges
lang-py