Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:将对象转换为数字时未调整大小的对象的len()_Python_Python 3.x_Pandas_Numeric - Fatal编程技术网

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

Python TypeError:将对象转换为数字时未调整大小的对象的len(),python,python-3.x,pandas,numeric,Python,Python 3.x,Pandas,Numeric,我正在处理一个数据帧,其列的形式如下: allHoldingsFund['ratioBest'] Out[72]: 65357 0.0 65371 0.0 65394 2.396777442094666 65397 0.0 65433 0.0167993412023058 65462 0.0 65560

我正在处理一个数据帧,其列的形式如下:

allHoldingsFund['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
pd.to_numeric(allHoldingsFund['ratioBest'])
回溯(最近一次呼叫最后一次):
文件“/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site packages/IPython/core/interactiveshell.py”,第2910行,运行代码
exec(代码对象、self.user\u全局、self.user\n)
文件“”,第1行,在
pd.to_numeric(allHoldingsFund['ratioBest'])
文件“/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site packages/pandas/core/tools/numeric.py”,第133行,输入到数值
强制\u数值=强制\u数值)
pandas.\u libs.lib.maybe\u convert\u numeric文件“pandas/_libs/src/expression.pyx”,第1111行
TypeError:未调整大小的对象的len()

我该如何解决这个问题呢?

对于我来说,使用nice参数
errors='concurve'
将no numeric转换为
NaN
s:

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

函数
to_numeric
将参数转换为数字类型。
只有在计算
int
值的
len()
之类的值时,才会出现未调整大小对象的
len()错误。因此,对您的错误的唯一解释是,无论您将列存储在什么数据结构中,它的部分或全部已经以
float
的形式存储,因为pandas正在尝试查找其长度并获取上述错误。

如何工作
allHoldingsFund['ratioBest']=pd.to_numeric(allHoldingsFund['ratioBest'],errors='compresse')
?@jezrael谢谢。它工作得很好。然而,它仍然是一个对象。它没有转换为浮动,这使得该列不能用于其他目的SSO
print(allHoldingsFund['ratioBest'].dtype)
再次返回
object
?yes
print(allHoldingsFund['ratioBest'].dtype)object
我还可以从这个不起作用的操作中分辨出来
np.where(allHoldingsFund['ratioBest']>1,1,0)
对我来说,它工作得很好我不同意,因为如果只浮动,那么OP应该得到
Name:ratioBest,Length:1664,dtype:float64
,而不是
Name:ratioBest,Length:1664,dtype:object
,如果只浮动
pd.\u numeric
工作得很好。在我看来,这似乎是个错误。
allHoldingsFund['ratioBest']
列是使用np.where创建的。基本上,如果值存在,那么我取这些值的比率,如果它们不存在,我只是将比率设置为零
allHoldingsFund['ratioBest']=np.where(如果存在值,则取比率0)
@jezrael为什么会出现错误?@SBad-因为出现了奇怪的错误。我的解决方案不起作用?我的答案完全基于堆栈跟踪,这就是你的问题:
我只是将比率设置为零。
。Pandas绝对是在尝试查找每个元素的长度,这就是您尝试查找
int 0
的长度时所导致的错误。试着让它成为
'0'
,你就可以开始了。