np.isnan用于Python中的数组

用户名

我有一个想要更改为True和False值的数组,以便可以删除nan值。

当使用np.isnan尝试如下时,这很好:

import numpy as np

a = np.array([1.,2.,np.nan])

a
Out[4]: array([ 1.,  2., nan])

np.isnan(a)
Out[5]: array([False, False,  True])

但是,当我尝试在阵列上执行相同操作时,它不起作用:

a
Out[9]: 
array([73788400000.0, 80017300000.0, 83680400000.0, 84939700000.0,
       83877800000.0, 83911700000.0, 85368100000.0, 83808200000.0,
       85936400000.0, 85177800000.0, 82705400000.0, 82119100000.0,
       73935400.0, 64018400.0, 42796500.0, 43130000.0, 42637600.0,
       167911000.0, nan], dtype=object)
np.isnan(a)

Traceback (most recent call last):

  File "<ipython-input-10-f4b5b5e7f347>", line 1, in <module>
    np.isnan(a)

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

我怀疑,鉴于错误,这与对象类型有关,但是我不确定到底有多精确。

请注意,尝试使用math.isnan时,它似乎仅采用单个值:

math.isnan(a)

Traceback (most recent call last):

  File "<ipython-input-11-6d4d8c26d370>", line 1, in <module>
    math.isnan(a)

TypeError: only size-1 arrays can be converted to Python scalars

任何帮助将不胜感激!

pp

将您的数组转换为float,这将起作用:

import numpy as np

a = np.array([73788400000.0, 80017300000.0, 83680400000.0, 84939700000.0,
              83877800000.0, 83911700000.0, 85368100000.0, 83808200000.0,
              85936400000.0, 85177800000.0, 82705400000.0, 82119100000.0,
              73935400.0, 64018400.0, 42796500.0, 43130000.0, 42637600.0,
              167911000.0, np.nan], dtype=object)

res = np.isnan(a.astype(float))

# [False False False False False False False False False False False False
#  False False False False False False  True]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章