我需要一個包含不同數據類型(浮點數或整數)列的表。我使用 dtype 來定義它們:import numpy as np# define arraydatadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<i4') ]arr = np.full((4,), np.nan, dtype=datadef) # fill array with dataarr['i'] = np.array([1, 2, 3, 4])arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])# nothing for 'j'print arr輸出 :[(1, 1.33333333, 2.77777778, -2147483648) (2, nan, 5.4 , -2147483648) (3, 2.66666667, 3.4 , -2147483648) (4, 5. , nan, -2147483648)]最后一列的NaN值已轉換為-2147483648,到目前為止沒有問題。但是現在我無法檢查我的數組中的值是否確實是 NaN :row = arr[1]print np.isnan(row) # 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''在單個單元格上,信息似乎NaN丟失了,-2147483648被認為是“經典數字”:print row # (2, nan, 5.4, -2147483648)print np.isnan(row[0]) # False, OKprint np.isnan(row[1]) # True, OKprint np.isnan(row[3]) # False, expected True在這種情況下是否有一種簡單的方法來檢查NaN整數?
Numpy:檢查整數 NaN
慕碼人2483693
2023-01-04 10:10:18