我try 了下面的代码,但它一直返回一个元组.我知道我可以使用requests图书馆,但我愿意使用图书馆.我正在使用的图书馆是here英镑.

from config import API_KEY
from alpha_vantage.fundamentaldata import FundamentalData
from alpha_vantage.alphavantage import AlphaVantage

if __name__ == '__main__':
    fd = FundamentalData(key=API_KEY, output_format='json')
    bs = fd.get_balance_sheet_annual(symbol='IBM')
    print(bs)

输出为:

(                              fiscalDateEnding  ... commonStockSharesOutstanding
date                                            ...                             
1970-01-01 00:00:00.000000000       2022-12-31  ...                    906091977
1970-01-01 00:00:00.000000001       2021-12-31  ...                    898068600
1970-01-01 00:00:00.000000002       2020-12-31  ...                    892653424
1970-01-01 00:00:00.000000003       2019-12-31  ...                    887110455
1970-01-01 00:00:00.000000004       2018-12-31  ...                    892479411

[5 rows x 38 columns], 'IBM')

推荐答案

该问题是库中的一个判断,该判断会将列表响应转换为Pandas数据帧,即使输出格式为‘json’而不是‘Pandas’.从https://github.com/RomelTorres/alpha_vantage/blob/9d8e9fe44c88ec46019f4d99ae829ce502f1465e/alpha_vantage/alphavantage.py#L239人起:

if output_format == 'json':
    if isinstance(data, list):
        # If the call returns a list, then we will append them
        # in the resulting data frame. If in the future
        # alphavantage decides to do more with returning arrays
        # this might become buggy. For now will do the trick.
        if not data:
            data_pandas = pandas.DataFrame()
        else:
            data_array = []
            for val in data:
                data_array.append([v for _, v in val.items()])
            data_pandas = pandas.DataFrame(data_array, columns=[
                k for k, _ in data[0].items()])
        return data_pandas, meta_data
    else:
        return data, meta_data
elif output_format == 'pandas':
    if isinstance(data, list):
        # If the call returns a list, then we will append them
        # in the resulting data frame. If in the future
        # alphavantage decides to do more with returning arrays
        # this might become buggy. For now will do the trick.
        if not data:
            data_pandas = pandas.DataFrame()
        else:
            data_array = []
            for val in data:
                data_array.append([v for _, v in val.items()])
            data_pandas = pandas.DataFrame(data_array, columns=[
                k for k, _ in data[0].items()])

这看起来像是一个错误,因为json格式不应该输出Pandas,并且代码直接重复下面的实际Pandas输出格式.

您可以在库中修补此代码,以便始终使用else子句,例如

if output_format == 'json':
    if isinstance(data, list) and False:
    # [...]
    else:
        return data, meta_data

然后,问题中的MCVE中的代码返回以下内容:

([{'fiscalDateEnding': '2022-12-31', 'reportedCurrency': 'USD', 'totalAssets': '127243000000', 'totalCurrentAssets': '29118000000', 'cashAndCashEquivalentsAtCarryingValue': '7886000000', 'cashAndShortTermInvestments': '7886000000', 'inventory': '1552000000', 'currentNetReceivables': '14209000000', 'totalNonCurrentAssets': '96874000000', 'propertyPlantEquipment': '5334000000', 'accumulatedDepreciationAmortizationPPE': '13361000000', 'intangibleAssets': '67133000000', 'intangibleAssetsExcludingGoodwill': '11184000000', 'goodwill': '55949000000', 'investments': 'None', 'longTermInvestments': '142000000', 'shortTermInvestments': '852000000', 'otherCurrentAssets': '2610000000', 'otherNonCurrentAssets': 'None', 'totalLiabilities': '105222000000', 'totalCurrentLiabilities': '31505000000', 'currentAccountsPayable': '4051000000', 'deferredRevenue': '15531000000', 'currentDebt': '9511000000', 'shortTermDebt': '4760000000', 'totalNonCurrentLiabilities': '83414000000', 'capitalLeaseObligations': '164000000', 'longTermDebt': '47190000000', 'currentLongTermDebt': '4676000000', 'longTermDebtNoncurrent': '46189000000', 'shortLongTermDebtTotal': '107759000000', 'otherCurrentLiabilities': '9788000000', 'otherNonCurrentLiabilities': '12243000000', 'totalShareholderEquity': '21944000000', 'treasuryStock': '169484000000', 'retainedEarnings': '149825000000', 'commonStock': '58343000000', 'commonStockSharesOutstanding': '906091977'}, {'fiscalDateEnding': '2021-12-31', 'reportedCurrency': 'USD', 'totalAssets': '132001000000', 'totalCurrentAssets': '29539000000', 'cashAndCashEquivalentsAtCarryingValue': '6650000000', 'cashAndShortTermInvestments': '6650000000', 'inventory': '1649000000', 'currentNetReceivables': '14977000000', 'totalNonCurrentAssets': '101786000000', 'propertyPlantEquipment': '5694000000', 'accumulatedDepreciationAmortizationPPE': '14390000000', 'intangibleAssets': '68154000000', 'intangibleAssetsExcludingGoodwill': '12511000000', 'goodwill': '55643000000', 'investments': '199000000', 'longTermInvestments': '159000000', 'shortTermInvestments': '600000000', 'otherCurrentAssets': '5663000000', 'otherNonCurrentAssets': 'None', 'totalLiabilities': '113005000000', 'totalCurrentLiabilities': '33619000000', 'currentAccountsPayable': '3955000000', 'deferredRevenue': '16095000000', 'currentDebt': '13551000000', 'shortTermDebt': '6787000000', 'totalNonCurrentLiabilities': '90188000000', 'capitalLeaseObligations': '63000000', 'longTermDebt': '56193000000', 'currentLongTermDebt': '6728000000', 'longTermDebtNoncurrent': '44917000000', 'shortLongTermDebtTotal': '110496000000', 'otherCurrentLiabilities': '9386000000', 'otherNonCurrentLiabilities': '13996000000', 'totalShareholderEquity': '18901000000', 'treasuryStock': '169392000000', 'retainedEarnings': '154209000000', 'commonStock': '57319000000', 'commonStockSharesOutstanding': '898068600'},
# lots more json data
], 'IBM')

Python相关问答推荐

如何处理嵌套的SON?

Chatgpt API不断返回错误:404未能从API获取响应

当密钥是复合且唯一时,Pandas合并抱怨标签不唯一

从webhook中的短代码(而不是电话号码)接收Twilio消息

Odoo 14 hr. emergency.public内的二进制字段

根据在同一数据框中的查找向数据框添加值

NP.round解算数据后NP.unique

如何请求使用Python将文件下载到带有登录名的门户网站?

如何使用表达式将字符串解压缩到Polars DataFrame中的多个列中?

未知依赖项pin—1阻止conda安装""

使用Python和文件进行模糊输出

使用Python从rotowire中抓取MLB每日阵容

在极中解析带有数字和SI前缀的字符串

python panda ExcelWriter切换动态公式到数组公式

python中csv. Dictreader. fieldname的类型是什么?'

OpenCV轮廓.很难找到给定图像的所需轮廓

循环浏览每个客户记录,以获取他们来自的第一个/最后一个渠道

Python Mercury离线安装

numpy数组和数组标量之间的不同行为

用fft计算指数复和代替求和来模拟衍射?