numpy整数nan
有没有一种方法可以将NaN存储在整数的Numpy数组中?我得到:
a=np.array([1],dtype=long)
a[0]=np.nan
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: cannot convert float NaN to integer
-
不,至少在当前版本的NumPy中不能。A
nan
是 仅对 float数组的特殊值。有人在谈论引入一个特殊的位,该位将允许非浮点数组存储实际上与a相对应的内容
nan
,但到目前为止(2012/10),这仅仅是个话题。同时,您可能需要考虑使用该
numpy.ma
程序包:您可以使用特殊numpy.ma.masked
值表示无效值,而不是选择像-99999这样的无效整数。a = np.ma.array([1,2,3,4,5], dtype=int) a[1] = np.ma.masked masked_array(data = [1 -- 3 4 5], mask = [False True False False False], fill_value = 999999)