Search code examples
pythonyahoo-finance

An empty dataframe in output


Updated post. I'm trying to get data from the balance sheet in Yahoo Fianance using yahoo_fin package. For downloading data I use the code below:

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

But when I'm trying to get data I have an empty dataframe in output.

And I've tried to get data using 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

But I have an error:

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'

I also tried to solve this problem as in Yfinance KeyError: 'regularMarketOpen' post but I didn't find raw "data = utils.get_json(url+'/financials', proxy)" in yfinance/base.py"


Solution

  • The dataframe recent_sheets[ticker] is an empty dataframe, thats why the axis has 0 elements. If you're trying to create a new dataframe with those columns, do

    for ticker in recent_sheets.keys():
        recent_sheets[ticker] = pd.DataFrame(columns=["9/29/2022", "9/29/2021", "9/29/2020"])