【发布时间】:2020-04-25 12:34:45
【问题描述】:
我遇到了 np.isnan() 的错误。可能是它打算以这种方式工作,问题是熊猫如何处理它。如果我制作一个混合类型的数据框,例如
raw_data = {'Binary 1': [True, True, False, False, True],
'Binary 2': [False, False, True, True, False],
'age': [42, 52, 36, 24, 73],
'preTestScore': [4, 24, 31, 2, 3],
'postTestScore': [25, 94, 57, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['Binary 1', 'Binary 2', 'age', 'preTestScore', 'postTestScore'])
df.dtypes
Binary 1 bool
Binary 2 bool
age int64
preTestScore int64
postTestScore int64
我不能打电话
np.isnan(df)
TypeError: 输入类型不支持 ufunc 'isnan',并且根据强制转换规则 ''safe'' 无法安全地将输入强制转换为任何支持的类型
这两个
np.isnan(df[['Binary 1', 'Binary 2']])
还有这个
np.isnan(df[['age', 'preTestScore', 'postTestScore']])
工作。我认为这是因为它们属于同一类型,因为这不是
np.isnan(df[['Binary 1', 'age']])
【问题讨论】:
-
我不认为这是一个错误,而是 numpy 不想将多种类型的数据强制转换为同一个数据类型,就像你说的那样。
-
轻松解决 --> 使用 print(pd.isnull(df[['Binary 1', 'age']])) 而不是 numpy