在 numpy 数组中查找 none 和 nan 的索引

finding index of none and nan in numpy array

我有一个数组,如下所示:

array([[  1.,   2.,   None],
       [ nan,   4.,   5.]])

我正在尝试以下操作:

np.equal(A, None) #works and finds index of None correctly
np.equal(A, np.nan) #doesn't work
np.isnan(A) #errors out

错误是:

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''

我应该如何处理这个问题,我试图在给定数组中找到 None 和 nan 的索引。

我的最终输出应如下所示:

array([[False, False,  True],
       [True, False, False]], dtype=bool)

我们可以先 cast the array 有 dtype float — 这会将 None 转换为 NaN。 numpy.isnan 然后可以在这个 float 数组上使用。

numpy.isnan(A.astype(float))