2014-02-14 51 views
2

我想添加兩個numpy數組,其中之一包含NoneType值。當然,當我加入他們,我得到這個錯誤:Numpy陣列加上NoneType

TypeError: unsupported operand type(s) for +: 'NoneType' and 'float' 

有沒有一種方法來定義NoneType之和浮動是NoneType,並保持它的新的數組中?

回答

4

如果None是,是允許的唯一的非數字值,那麼你可能想使用NaN的,而不是來代表它:

>>> x = np.ones(4) 
>>> y = np.array([1., 2., None, 4.], dtype=np.float) 
>>> x + y 
array([ 2., 3., nan, 5.]) 
+0

這解決了我的問題。謝謝。 – mcglashan