将请求的列表从 api 转换为数据帧 属性错误:'list'对象没有属性'keys'



试图从eod api eod日内api 获取几年的日内历史数据

from datetime import datetime
dates=[ '01-01-2021','01-04-2021','01-07-2021','01-10-2021',
'01-01-2022','01-04-2022','01-07-2022']
# Convert your strings to datetime, using `datetime` library
dates = [datetime.strptime(date, "%d-%m-%Y") for date in dates]

def create_df(pair,dates):
df = []
for index, elem in enumerate(dates):
if index== 0:
curr_date = str(elem.timestamp())
next_date = str(dates[index+1].timestamp())
df = client.get_prices_intraday(pair, interval = '1m', from_ = curr_date, to = next_date)
elif ((index>0) & (index+1 < len(dates))):
curr_date = str(elem.timestamp())
next_date = str(dates[index+1].timestamp())
df2 = client.get_prices_intraday(pair, interval = '1m', from_ = curr_date, to = next_date)
df.append(df2)
return df



from eod import EodHistoricalData
# create the instance of the SDK
api_key = 'my_api_key'
client = EodHistoricalData(api_key)


GBPAUD = create_df('GBPAUD.FOREX',dates)

是什么给了我这样的东西:

GBPAUD

[{'timestamp': 1609693200,
'gmtoffset': 0,
'datetime': '2021-01-03 17:00:00',
'open': 1.77086,
'high': 1.77086,
'low': 1.77086,
'close': 1.77086,
'volume': 1},
{'timestamp': 1609693260,
'gmtoffset': 0,
'datetime': '2021-01-03 17:01:00',
'open': 1.77086,
'high': 1.77086,
'low': 1.77086,
'close': 1.77086,
'volume': 1},
{'timestamp': 1609693320,
'gmtoffset': 0,
'datetime': '2021-01-03 17:02:00',
'open': 1.77086,
'high': 1.77086,
'low': 1.77086,
'close': 1.77086,
'volume': 1},
{'timestamp': 1609693380,
'gmtoffset': 0,
'datetime': '2021-01-03 17:03:00',
'open': 1.77086,
'high': 1.77222,
'low': 1.77086,
'close': 1.77199,
'volume': 14},
{'timestamp': 1609693440,
'gmtoffset': 0,
'datetime': '2021-01-03 17:04:00',
'open': 1.77203,
'high': 1.77348,
'low': 1.77176,
'close': 1.77199,
'volume': 23},

它被存储为一个列表,但当我试图转换为pandas数据帧时:

GBPAUD = pd.DataFrame(GBPAUD)

----------------------------------------------------------------------------AttributeError Traceback(最近调用最后(输入In[39],在<细胞系:1>((---->1 GBPAUD=pd.DataFrame(GBPAUD(

文件~/anconda3/envs/rapids-22.02/lib/python3.9/site packages/pandas/core/frameter.py:694,在DataFrame中init(self、data、index、columns、dtype、copy(689如果列不是"无":690#错误:参数1到";ensure_index";具有不兼容的类型691#";集合[Any]";;预期的";Union[Unination[Unination[ExtensionArray,692"ndarray],Index,Series],Sequence[Any]]";693 columns=ensure_index(columns(#type:ignore[arg type]-->694个数组,列,索引=嵌套数据_数组(695#错误:参数3为"0";nested_data_to_arrays";具有不兼容696#型";Optional[Collection[Any]]";;预期的";可选[索引]";697个数据,698列,699索引,#type:ignore[arg-type]700d型,701(702 mgr=阵列_to_mgr(703阵列,704列,(…(708 typ=管理器,709(710其他:

文件~/anconda3/envs/rapids-22.02/lib/python3.9/site packages/pandas/core/internites/construction.py:483,在nested_data_to_arrays中(数据、列、索引、数据类型(480如果is_named_tuple(data[0](和columns为None:481列=ensure_index(data[0].fields(-->483个数组,列=到数组(数据,列,数据类型=数据类型(484列=ensure_index(列(486如果索引为None:

文件~/anconda3/envs/rapids-22.02/lib/python3.9/site packages/pandas/core/internites/construction.py:799,在to_arrays(数据、列、数据类型(中797 arr=_list_to_arrays(数据(798 elif isistance(数据[0],abc.Mapping(:-->799 arr,columns=_list_of_dict_to_arrays(数据,列(800 elif isistance(数据[0],ABC系列(:801 arr,columns=_list_of_series_to_arrays(数据,columns(

文件~/anconda3/envs/rapids-22.02/lib/python3.9/site packages/pandas/core/internites/construction.py:884,在_list_of_dict_to_arrays(数据,列(中882 gen=(数据中x的list(x.keys((((883 sort=没有(数据中d的isistance(d,dict((-->884 pre_cols=lib.fast_unique_multiple_list_gen(gen,sort=sort(885列=ensure_index(pre_cols(887#确保它们属于基本dict类,而不是派生的888#类

文件~/anconda3/envs/rapids-22.02/lib/python3.9/site packages/pandas/libs/lib.pyx:400,大熊猫_libs.lib.fast_unique_multiple_list_gen((

文件~/anconda3/envs/rapids-22.02/lib/python3.9/site packages/pandas/core/internites/construction.py:882,在(.0(862〃"quot;863将dicts列表转换为numpy数组864(…(879列:索引880〃"quot;881如果列为"无":-->882 gen=(数据中x的list(x.keys((((883 sort=没有(数据中d的isistance(d,dict((884 pre_cols=lib.fast_unique_multiple_list_gen(gen,sort=sort(

AttributeError:"list"对象没有属性"keys">

有谁能用更优雅的方法从该api中获取大量数据,或者修复错误?

感谢

您的示例数据正确读取到panda中(关闭列表后(。在for循环中,在不查看数据的情况下(请共享一个片段而不是一个链接来注册服务(,您将以两种不同的方式将数据添加到df变量中,这将在单个列表和无键之后创建一个列表列表列表。这就是熊猫所抱怨的。在下面的例子中,它有一个最小化版本的代码,并使用您的数据作为输入,观察以下输出:

data = [{'timestamp': 1609693200,
'gmtoffset': 0,
'datetime': '2021-01-03 17:00:00',
'open': 1.77086,
'high': 1.77086,
'low': 1.77086,
'close': 1.77086,
'volume': 1},
{'timestamp': 1609693260,
'gmtoffset': 0,
'datetime': '2021-01-03 17:01:00',
'open': 1.77086,
'high': 1.77086,
'low': 1.77086,
'close': 1.77086,
'volume': 1},
{'timestamp': 1609693320,
'gmtoffset': 0,
'datetime': '2021-01-03 17:02:00',
'open': 1.77086,
'high': 1.77086,
'low': 1.77086,
'close': 1.77086,
'volume': 1},
{'timestamp': 1609693380,
'gmtoffset': 0,
'datetime': '2021-01-03 17:03:00',
'open': 1.77086,
'high': 1.77222,
'low': 1.77086,
'close': 1.77199,
'volume': 14},
{'timestamp': 1609693440,
'gmtoffset': 0,
'datetime': '2021-01-03 17:04:00',
'open': 1.77203,
'high': 1.77348,
'low': 1.77176,
'close': 1.77199,
'volume': 23}]
df = []
dates = [1,2,3]
for index, elem in enumerate(dates):
if index== 0:
# df = client.get_prices_intraday(pair, interval = '1m', from_ = curr_date, to = next_date)
df = data
elif ((index>0) & (index+1 < len(dates))):
# df2 = client.get_prices_intraday(pair, interval = '1m', from_ = curr_date, to = next_date)
df2 = data
df.append(df2)
print(df)

你的输出是:

[{'timestamp': 1609693200, 'gmtoffset': 0, 'datetime': '2021-01-03 17:00:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693260, 'gmtoffset': 0, 'datetime': '2021-01-03 17:01:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693320, 'gmtoffset': 0, 'datetime': '2021-01-03 17:02:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693380, 'gmtoffset': 0, 'datetime': '2021-01-03 17:03:00', 'open': 1.77086, 'high': 1.77222, 'low': 1.77086, 'close': 1.77199, 'volume': 14}, {'timestamp': 1609693440, 'gmtoffset': 0, 'datetime': '2021-01-03 17:04:00', 'open': 1.77203, 'high': 1.77348, 'low': 1.77176, 'close': 1.77199, 'volume': 23}, [...], [...]]

看到最后的两个列表了吗?我想你想要的是,使用相同的列表数据,这个

df = []
dates = [1,2,3]
for date in dates:
df += data
print(df)

它输出这个:

[{'timestamp': 1609693200, 'gmtoffset': 0, 'datetime': '2021-01-03 17:00:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693260, 'gmtoffset': 0, 'datetime': '2021-01-03 17:01:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693320, 'gmtoffset': 0, 'datetime': '2021-01-03 17:02:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693380, 'gmtoffset': 0, 'datetime': '2021-01-03 17:03:00', 'open': 1.77086, 'high': 1.77222, 'low': 1.77086, 'close': 1.77199, 'volume': 14}, {'timestamp': 1609693440, 'gmtoffset': 0, 'datetime': '2021-01-03 17:04:00', 'open': 1.77203, 'high': 1.77348, 'low': 1.77176, 'close': 1.77199, 'volume': 23}, {'timestamp': 1609693200, 'gmtoffset': 0, 'datetime': '2021-01-03 17:00:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693260, 'gmtoffset': 0, 'datetime': '2021-01-03 17:01:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693320, 'gmtoffset': 0, 'datetime': '2021-01-03 17:02:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693380, 'gmtoffset': 0, 'datetime': '2021-01-03 17:03:00', 'open': 1.77086, 'high': 1.77222, 'low': 1.77086, 'close': 1.77199, 'volume': 14}, {'timestamp': 1609693440, 'gmtoffset': 0, 'datetime': '2021-01-03 17:04:00', 'open': 1.77203, 'high': 1.77348, 'low': 1.77176, 'close': 1.77199, 'volume': 23}, {'timestamp': 1609693200, 'gmtoffset': 0, 'datetime': '2021-01-03 17:00:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693260, 'gmtoffset': 0, 'datetime': '2021-01-03 17:01:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693320, 'gmtoffset': 0, 'datetime': '2021-01-03 17:02:00', 'open': 1.77086, 'high': 1.77086, 'low': 1.77086, 'close': 1.77086, 'volume': 1}, {'timestamp': 1609693380, 'gmtoffset': 0, 'datetime': '2021-01-03 17:03:00', 'open': 1.77086, 'high': 1.77222, 'low': 1.77086, 'close': 1.77199, 'volume': 14}, {'timestamp': 1609693440, 'gmtoffset': 0, 'datetime': '2021-01-03 17:04:00', 'open': 1.77203, 'high': 1.77348, 'low': 1.77176, 'close': 1.77199, 'volume': 23}]

读熊猫或大棒都很好。

最新更新