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

时间:2017-03-14 19:47:41

标签: python numpy nan

我有一个看起来像的数组:

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)

1 个答案:

答案 0 :(得分:3)

我们可以先cast the array让dtype float - 这会将None转换为NaN。然后可以在此float数组上使用Label m = new Label(messageText); m.setFont(Font.font("Calibri", FontWeight.SEMI_BOLD, 20)); m.setWrapText(true); m.setAlignment(Pos.CENTER); m.setTextAlignment(TextAlignment.CENTER); m.setMaxWidth(FlowPane.USE_PREF_SIZE); //Works with constants only

numpy.isnan
相关问题