将数据附加到Pandas数据框时出现错误消息 [英] Error message when appending data to pandas dataframe

查看:68
本文介绍了将数据附加到Pandas数据框时出现错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我这个忙吗

我创建了一个循环,以追加Coinbase中历史价格数据的连续间隔.

我的循环成功迭代了几次,然后崩溃了.

错误消息(在data_temp代码行下):

"ValueError:如果使用所有标量值,则必须传递索引"

  days = 10结束= datetime.now().replace(微秒= 0)开始=结束-timedelta(天=天)data_price = pd.DataFrame()对于范围在(1,50)中的我:打印(开始)打印(完)data_temp = pd.DataFrame(public_client.get_product_historic_rates(product_id ='BTC-USD',粒度= 3600,开始=开始,结束=结束))data_price = data_price.append(data_temp)结束=开始开始=结束-timedelta(天=天) 

很想了解如何解决此问题以及为什么会首先发生这种情况.

谢谢!

这是完整的踪迹:

回溯(最近通话最近):文件"\ coinbase_bot.py",第46行,在data_temp = pd.DataFrame(public_client.get_product_historic_rates(product_id ='BTC-USD',粒度= 3600,开始=开始,结束=结束)) init 中的文件"D:\ Program Files \ Python37 \ lib \ site-packages \ pandas \ core \ frame.py",第411行mgr = init_dict(数据,索引,列,dtype = dtype)init_dict中的文件"D:\ Program Files \ Python37 \ lib \ site-packages \ pandas \ core \ internals \ construction.py",第257行返回arrays_to_mgr(数组,数据名称,索引,列,dtype = dtype)文件"D:\ Program Files \ Python37 \ lib \ site-packages \ pandas \ core \ internals \ construction.py",行77,在arrays_to_mgr中索引= extract_index(数组)文件"D:\ Program Files \ Python37 \ lib \ site-packages \ pandas \ core \ internals \ construction.py",第358行,在extract_index中引发ValueError(如果使用所有标量值,则必须传递索引")ValueError:如果使用所有标量值,则必须传递索引

这里的json是通过简单的url调用返回的:

[[1454716800,370.05,384.54,384.44,375.44,6276.66473729],[1454630400,382.99,389.36,387.99,384.5,7443.92933224],[1454544000,368.74,390.63,368.87,387.99,8887.7572324],[1454457600,365.63,373.01,372.93,368.87,7147.95657328],[1454371200,371.17,374.41,371.33,372.93,6856.21815799],[1454284800,366.26,379,367.89,371.33,7931.22922922],[1454198400,365,382.5,378.46,367.95,5506.7768]>

与该用户的问题非常相似,但无法动弹:在尝试合并多个数据框时,如何解决"ValueError:如果使用所有标量值,则必须传递索引"

解决方案

-DashOfProgramming,您好

您的问题是 data_temp 仅初始化为一行,而pandas要求您为其提供索引.

以下代码段可以解决此问题.我用一个简单的字典替换了您的API调用,该字典类似于我期望API返回的字典,并使用 i 作为数据帧的索引(这具有您也可以跟踪的优点):

 将pandas导入为pd从datetime导入datetime,timedelta天= 10结束= datetime.now().replace(微秒= 0)开始=结束-timedelta(天=天)data_price = pd.DataFrame()temp_dict = {'开始':'2019-09-30','结束':'2019-10-01','价格':'-111.0928','货币:美元'}对于范围在(1,50)中的我:打印(开始)打印(完)data_temp = pd.DataFrame(temp_dict,index = [i])data_price = data_price.append(data_temp)结束=开始开始=结束-timedelta(天=天)打印(data_price) 


编辑

刚刚看到您的API输出是一个嵌套列表.pd.DataFrame()认为该列表仅是一行,因为它是嵌套的.我建议您将列存储在单独的变量中,然后执行以下操作:

  cols = ['ts','low','high','open','close','sth_else']v = [[...],[...],[...]]#您的清单清单data_temp = pd.DataFrame.from_records(v,column = cols) 

Can someone give me a hand with this:

I created a loop to append successive intervals of historical price data from Coinbase.

My loop iterates successfully a few times then crashes.

Error message (under data_temp code line):

"ValueError: If using all scalar values, you must pass an index"

days = 10
end = datetime.now().replace(microsecond=0)
start = end - timedelta(days=days)
data_price = pd.DataFrame()

for i in range(1,50):
    print(start)
    print(end)
    data_temp = pd.DataFrame(public_client.get_product_historic_rates(product_id='BTC-USD', granularity=3600, start=start, end=end))
    data_price = data_price.append(data_temp)
    end = start
    start = end - timedelta(days=days)

Would love to understand how to fix this and why this is happening in the first place.

Thank you!

Here's the full trace:

Traceback (most recent call last): File "\coinbase_bot.py", line 46, in data_temp = pd.DataFrame(public_client.get_product_historic_rates(product_id='BTC-USD', granularity=3600, start=start, end=end)) File "D:\Program Files\Python37\lib\site-packages\pandas\core\frame.py", line 411, in init mgr = init_dict(data, index, columns, dtype=dtype) File "D:\Program Files\Python37\lib\site-packages\pandas\core\internals\construction.py", line 257, in init_dict return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype) File "D:\Program Files\Python37\lib\site-packages\pandas\core\internals\construction.py", line 77, in arrays_to_mgr index = extract_index(arrays) File "D:\Program Files\Python37\lib\site-packages\pandas\core\internals\construction.py", line 358, in extract_index raise ValueError("If using all scalar values, you must pass an index") ValueError: If using all scalar values, you must pass an index

Here's json returned via simple url call:

[[1454716800,370.05,384.54,384.44,375.44,6276.66473729],[1454630400,382.99,389.36,387.99,384.5,7443.92933224],[1454544000,368.74,390.63,368.87,387.99,8887.7572324],[1454457600,365.63,373.01,372.93,368.87,7147.95657328],[1454371200,371.17,374.41,371.33,372.93,6856.21815799],[1454284800,366.26,379,367.89,371.33,7931.22922922],[1454198400,365,382.5,378.46,367.95,5506.77681302]]

Very similar to this user's issue but cannot put my finger on it: When attempting to merge multiple dataframes, how to resolve "ValueError: If using all scalar values, you must pass an index"

解决方案

-- Hi DashOfProgramming,

Your problem is that the data_temp is initialised with only a single row and pandas requires you to provide it with an index for that.

The following snippet should resolve this. I replaced your API call with a simple dictionary that resembles what I would expect the API to return and used i as index for the dataframe (this has the advantage that you can keep track as well):

import pandas as pd
from datetime import datetime, timedelta

days = 10
end = datetime.now().replace(microsecond=0)
start = end - timedelta(days=days)
data_price = pd.DataFrame()

temp_dict = {'start': '2019-09-30', 'end': '2019-10-01', 'price': '-111.0928', 
'currency': 'USD'}

for i in range(1,50):
  print(start)
  print(end)
  data_temp = pd.DataFrame(temp_dict, index=[i])
  data_price = data_price.append(data_temp)
  end = start
  start = end - timedelta(days=days)

print(data_price)


EDIT

Just saw that your API output is a nested list. pd.DataFrame() thinks the list is only one row, because it's nested. I suggest you store your columns in a separate variable and then do this:

cols = ['ts', 'low', 'high', 'open', 'close', 'sth_else']

v = [[...], [...], [...]] # your list of lists

data_temp = pd.DataFrame.from_records(v, columns=cols)

这篇关于将数据附加到Pandas数据框时出现错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆