【问题标题】:Converting list requested from api to dataframe AttributeError: 'list' object has no attribute 'keys'将从 api 请求的列表转换为数据框 AttributeError:\'list\' 对象没有属性 \'keys\'
【发布时间】:2022-08-19 22:08:39
【问题描述】:

试图从 eod api eod intraday 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},

存储为列表,但是当我尝试转换为熊猫数据框时:

GBPAUD = pd.DataFrame(GBPAUD)

-------------------------------------------------- ------------------------- AttributeError Traceback(最近调用 last) 在 [39] 中输入 <cell line: 1>() ----> 1 GBPAUD = pd.DataFrame(GBPAUD)

文件 ~/anaconda3/envs/rapids-22.02/lib/python3.9/site-packages/pandas/core/frame.py:694, 在数据帧中。在里面(自我、数据、索引、列、dtype、副本) 689 如果列不是无: 690 # 错误:“ensure_index”的参数 1 的类型不兼容 第691章预期 \"Union[Union[Union[ExtensionArray, 第692章 693 列 = 确保索引(列)# 类型:忽略 [arg 类型] --> 694 个数组、列、索引 = nested_data_to_arrays( 695 # 错误:“nested_data_to_arrays”的参数 3 不兼容 第696章预期 \"可选[索引]\" 697个数据, 698列, 699 索引,# 类型:忽略 [arg 类型] 700 dtype, 701) 第702章 703 个数组, 704 列,(...) 708 类型=经理, 709) 710 其他:

文件 ~/anaconda3/envs/rapids-22.02/lib/python3.9/site-packages/pandas/core/internals/construction.py:483, 在nested_data_to_arrays(数据,列,索引,dtype) 480 如果 is_named_tuple(data[0]) 并且列是无: 481 列 = 确保索引(数据 [0]._fields) --> 483 个数组,columns = to_arrays(data, columns, dtype=dtype) 484 列 = 确保索引(列) 如果索引为无,则为 486:

文件 ~/anaconda3/envs/rapids-22.02/lib/python3.9/site-packages/pandas/core/internals/construction.py:799, 在 to_arrays(数据,列,dtype) 第797章 第798章 --> 799 arr,列 = _list_of_dict_to_arrays(数据,列) 800 elif isinstance(数据[0],ABCSeries): 801 arr,列=_list_of_series_to_arrays(数据,列)

文件 ~/anaconda3/envs/rapids-22.02/lib/python3.9/site-packages/pandas/core/internals/construction.py:884, 在_list_of_dict_to_arrays(数据,列) 第882章 第883章 --> 884 pre_cols = lib.fast_unique_multiple_list_gen(gen, sort=sort) 885 列 = 确保索引(pre_cols) 第887章 888#班

文件 ~/anaconda3/envs/rapids-22.02/lib/python3.9/site-packages/pandas/_libs/lib.pyx:400, 在 pandas._libs.lib.fast_unique_multiple_list_gen()

文件 ~/anaconda3/envs/rapids-22.02/lib/python3.9/site-packages/pandas/core/internals/construction.py:882, 在 (.0) 第862章 863 将字典列表转换为 numpy 数组 864 (...) 879 列:索引 第880章 881 如果列是无: --> 882 gen = (list(x.keys()) for x in data) 第883章 第884章

AttributeError: \'list\' 对象没有属性 \'keys\'

任何人都有更优雅的方式从该 api 获取大量数据,或者修复错误的方法?

谢谢

    标签: python-3.x pandas api


    【解决方案1】:

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

    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}]
    

    并读入 pandas 或 cudf 就好了。

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 2021-07-26
      • 2021-08-09
      • 2021-05-05
      • 2020-06-12
      • 1970-01-01
      • 2021-06-17
      • 2021-06-11
      • 1970-01-01
      相关资源
      最近更新 更多