All, I've cut my teeth making a stock assessment python script in google colab (largely thanks to this community). 9/10 times, the code works perfectly to update a corresponding google sheets file. However, select stocks I use with the code have an interesting result. Rather than providing a table of earnings, it returns an empty DataFrame with columns for Open, Close, etc, aka, nothing having to do with earnings. Some sample tickers include PLTR, TRUP, and FICO. I'm curious if there is any cause for this other than just a bad data set for the given stocks OR any tips on how to handle this issue.
Returned result: Empty DataFrame Columns: [Open, High, Low, Close, Adj Close, Volume] Index: []
Sample code:
!pip install git+https://github.com/rodrigobercini/yfinance.git
!pip install gspread-formatting
import yfinance as yf, datetime, pandas as pd, gspread, gspread_formatting as gsf
from datetime import date
################################
stock = yf.Ticker("pltr")
info = stock.info
print(stock.earnings)
it seems that this library uses Request to get the HTML page for each ticker, and doesn't use API (seems yahoo finance API is not available anymore). so there is a possibility the connection is failed. you can use a loop to try for the MAX_NUM_TRY number in case the dataframe is empty.
MAX_NUM_TRY = 10
for _ in range(MAX_NUM_TRY):
stock = yf.Ticker("pltr")
info = stock.info
if len(stock.history()) > 0:
break