pandas-11 TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely错误解决方法

pandas-11 TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely错误解决方法

将一个list矩阵转化为numpy数组之后,使用np.isnan()方法,报出了这么一个错误:

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

经过测试,发现是二维list转化为numpy数组的时候就存在问题了。
出错原因是list第二维的元素个数不一致导致如:
list1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9, 10]]
这时候使用 np.array(list1),输出如下内容:
[
list(1, 2, 3),
list(4, 5, 6),
list(7, 8, 9, 10)
]
接着使用np.isnan()方法,就会报出最上面的错误。
因为第三行是4个元素,所以无法转化为我们想要的numpy数组,解决方法就是将列数改为一致即可。

原文地址:https://www.cnblogs.com/wenqiangit/p/11252776.html