长度不匹配:预期轴有 0 个元素,新值有 3 个元素

问题描述 投票:0回答:1

更新帖子。 我正在尝试使用 yahoo_fin 包从 Yahoo Fianance 的资产负债表中获取数据。我使用以下代码下载数据:

import yahoo_fin.stock_info as si
import pandas as pd
import requests
import requests_html

ticker_list = ["amzn", "aapl"]

# Get data in the current column for each stock's valuation table
balance_sheets = {}
for ticker in ticker_list:
    balance_sheets[ticker] = si.get_balance_sheet(ticker)
    
recent_sheets = {ticker : sheet.iloc[:,:1] for ticker,sheet in balance_sheets.items()}
 
for ticker in recent_sheets.keys():
     recent_sheets[ticker] = pd.DataFrame(columns=["9/29/2022", "9/29/2021", "9/29/2020"])
 # combine all balance sheets together
combined_sheets = pd.concat(recent_sheets)
 
# reset index to pull in ticker
combined_sheets = combined_sheets.reset_index()
 
# update column names
combined_sheets.columns = ["Ticker", "Breakdown", "9/29/2022", "9/29/2021", "9/29/2020"]

但是当我试图获取数据时,输出中有一个空数据框。

我尝试使用 yfinance 获取数据:

import yfinance as yf
import pandas as pd

fang = ['FB','AMZN','NFLX','GOOG']
tickers = [yf.Ticker(ticker) for ticker in fang]
dfs = [] # list for each ticker's dataframe
for ticker in tickers:
    # get each financial statement
    pnl = ticker.financials
    bs = ticker.balancesheet
    cf = ticker.cashflow

    # concatenate into one dataframe
    fs = pd.concat([pnl, bs, cf])

    # make dataframe format nicer
    # Swap dates and columns
    data = fs.T
    # reset index (date) into a column
    data = data.reset_index()
    # Rename old index from '' to Date
    data.columns = ['Date', *data.columns[1:]]
    # Add ticker to dataframe
    data['Ticker'] = ticker.ticker
    dfs.append(data)
data.iloc[:,:3]# for display purposes

但是我有一个错误:

KeyError                                  Traceback (most recent call last)
Input In [5], in <cell line: 8>()
      7 dfs = [] # list for each ticker's dataframe
      8 for ticker in tickers:
      9     # get each financial statement
---> 10     pnl = ticker.financials
     11     bs = ticker.balancesheet
     12     cf = ticker.cashflow
KeyError: 'regularMarketOpen'

我也尝试解决这个问题,如 Yfinance KeyError: 'regularMarketOpen' post 但我没有找到原始的“

data = utils.get_json(url+'/financials', proxy)" in yfinance/base.py"

python yahoo-finance
1个回答
1
投票

数据框

recent_sheets[ticker]
是一个空数据框,这就是为什么轴有0个元素。如果您尝试使用这些列创建一个新的数据框,请执行

for ticker in recent_sheets.keys():
    recent_sheets[ticker] = pd.DataFrame(columns=["9/29/2022", "9/29/2021", "9/29/2020"])
© www.soinside.com 2019 - 2024. All rights reserved.