Python Numpy 整数 nan
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12708807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Numpy integer nan
提问by Yariv
Is there a way to store NaN in a Numpy array of integers? I get:
有没有办法将 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
采纳答案by Pierre GM
No, you can't, at least with current version of NumPy. A nanis a special value for float arrays only.
不,您不能,至少使用当前版本的 NumPy。Anan是仅用于浮点数组的特殊值。
There are talks about introducing a special bit that would allow non-float arrays to store what in practice would correspond to a nan, but so far (2012/10), it's only talks.
有关于引入一个特殊位的讨论,该位将允许非浮点数组存储实际上对应于 a 的内容nan,但到目前为止(2012/10),这只是讨论。
In the meantime, you may want to consider the numpy.mapackage: instead of picking an invalid integer like -99999, you could use the special numpy.ma.maskedvalue to represent an invalid value.
同时,您可能需要考虑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)
回答by Julian
A nan is a floating point only thing, there is no representation of it in the integers, so no :)
nan 只是一个浮点数,整数中没有它的表示,所以没有:)
Pick an invalid value, like -99999
选择一个无效值,例如 -99999

