代码之家  ›  专栏  ›  技术社区  ›  SBad

TypeError:将对象转换为数字时未调整大小的对象的len()

  •  0
  • SBad  · 技术社区  · 6 年前

    所有控股基金['ratioBest']

    Out[72]: 
    65357                     0.0
    65371                     0.0
    65394       2.396777442094666
    65397                     0.0
    65433      0.0167993412023058
    65462                     0.0
    65560                     0.0
    Name: ratioBest, Length: 1664, dtype: object
    

    列是一个对象,我通常使用 allHoldingsFund['ratioBest']=pd.to_numeric(allHoldingsFund['ratioBest'])

    但是,当我这样做时,我得到了一个我无法解决的错误:

    pd.to_numeric(allHoldingsFund['ratioBest'])
    Traceback (most recent call last):
      File "/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-71-6f0ccaf63f24>", line 1, in <module>
        pd.to_numeric(allHoldingsFund['ratioBest'])
      File "/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site-packages/pandas/core/tools/numeric.py", line 133, in to_numeric
        coerce_numeric=coerce_numeric)
      File "pandas/_libs/src/inference.pyx", line 1111, in pandas._libs.lib.maybe_convert_numeric
    TypeError: len() of unsized object
    

    我怎样才能解决这个问题?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Ishan Srivastava    6 年前

    函数 to_numeric 将参数转换为数字类型。 len() of unsized object len() 一个 int float 因为熊猫正试图找到它的长度并得到上面的错误。

        2
  •  0
  •   felice    4 年前

    对我来说工作很好 errors='coerce' NaN 学生:

    allHoldingsFund = pd.DataFrame({'ratioBest':[['123'], '123', 'aaa']})
    
    allHoldingsFund['ratioBest'] = pd.to_numeric(allHoldingsFund['ratioBest'], errors='coerce')
    print (allHoldingsFund)
       ratioBest
    0        NaN
    1      123.0
    2        NaN
    
    print (allHoldingsFund['ratioBest'].dtypes)
    float64