Numpy 整数 nan [英] Numpy integer nan

查看:33
本文介绍了Numpy 整数 nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 NaN 存储在 Numpy 整数数组中?我得到:

Is there a way to store NaN in a Numpy array of integers? I get:

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 中是这样.nan 是浮点数组的特殊值.

No, you can't, at least with current version of NumPy. A nan is a special value for float arrays only.

有一些关于引入一个特殊位的讨论,该位允许非浮点数组存储实际上对应于 nan 的内容,但到目前为止(2012/10),这只是讨论.

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.

与此同时,您可能需要考虑 numpy.ma 包:您可以使用特殊的 numpy.ma.masked 值来表示无效值,而不是选择像 -99999 这样的无效整数.

In the meantime, you may want to consider the numpy.ma package: instead of picking an invalid integer like -99999, you could use the special numpy.ma.masked value to represent an invalid value.

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)

这篇关于Numpy 整数 nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆