该问题是库中的一个判断,该判断会将列表响应转换为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')