【问题标题】:TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported typesTypeError:输入类型不支持 ufunc 'isfinite',并且输入无法安全地强制转换为任何支持的类型
【发布时间】:2019-12-04 00:50:59
【问题描述】:

运行此行后,我收到以下错误: TypeError: 输入类型不支持 ufunc 'isfinite',并且根据强制转换规则 ''safe'' 无法安全地将输入强制转换为任何支持的类型

df[~(  (pd.np.isclose(df.Sizes.round(3), df.Volumes))  & (df['color'] == 'Blue')   )] 

我该如何解决?

【问题讨论】:

  • isclose 仅适用于数字 - isfinite 测试适用于浮点数。 df.Sizesdf.Volumes 的 dtype 是什么?
  • 都是'float64'
  • 如果您能告诉我们这些论点的全部内容,那么这就是我们可以为您提供的全部帮助!
  • 它们是数字,也包括小数,例如 (8.234),当舍入列大小时,将得到的舍入数字与卷列中的数字进行比较,如果它们相等,则删除该行。

标签: python-3.x numpy


【解决方案1】:
In [152]: df=pd.DataFrame(np.arange(12.).reshape(4,3), columns=['one','two','three'])                        
In [153]: df.three[:] = ['a',np.nan,4,3.2]                                                                   
In [154]: df                                                                                                 
Out[154]: 
   one   two three
0  0.0   1.0     a
1  3.0   4.0   nan
2  6.0   7.0     4
3  9.0  10.0   3.2
In [155]: pd.np.isclose(df.one.round(3), df.two)                                                             
Out[155]: array([False, False, False, False])
In [156]: pd.np.isclose(df.one.round(3), df.three)                                                           
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-156-d99a19b1d9b5> in <module>
----> 1 pd.np.isclose(df.one.round(3), df.three)

/usr/local/lib/python3.6/dist-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
   2520 
   2521     xfin = isfinite(x)
-> 2522     yfin = isfinite(y)
   2523     if all(xfin) and all(yfin):
   2524         return within_tol(x, y, atol, rtol)

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

对两个 float32 列的测试有效,但当一列是具有非数字值的对象 dtype 时会引发此错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-29
    • 2019-03-10
    • 2022-08-20
    • 2020-02-16
    • 2019-11-04
    • 1970-01-01
    • 2022-12-17
    • 2020-07-21
    相关资源
    最近更新 更多