【问题标题】:How to pull information from an object from an API call?如何通过 API 调用从对象中提取信息?
【发布时间】:2021-02-04 00:11:58
【问题描述】:

我正在尝试从返回我不熟悉的对象的 API 调用中解析数据。我正在使用 lpaca.markets Assets 调用所有可交易资产(股票)。 API 以资产实体的形式提供 9,322 种资产。

Asset({'class': 'us_equity',
       'easy_to_borrow': True,
       'exchange': 'NASDAQ',
       'id': 'ea7afd19-72b9-48cf-9f8b-1d9738870a24',
       'marginable': True,
       'name': 'IEC Electronics Corp. Common Stock',
       'shortable': True,
       'status': 'active',
       'symbol': 'IEC',
       'tradable': True})]

我想将 Asset 对象中的字典转换为 9,322 行的 pandas 数据框。当我尝试这样做时,我得到了错误:

AttributeError: 'Asset' object has no attribute 'Asset'

TypeError: 'Asset' object is not subscriptable

我该怎么办?

【问题讨论】:

    标签: python api dataframe object stock


    【解决方案1】:

    我能够使用以下方法从 Asset 对象中提取项目:

    i.__getattr__('symbol')
    

    然后可以将它们添加到我的数据框中:

    .append()
    

    但是,这有点慢。有没有比追加更快的方法?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-25
      • 2015-11-22
      • 1970-01-01
      • 2020-04-16
      • 2012-07-02
      • 2015-05-25
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多