'YFinance+Pandas Hanging

I have a script that reads a list of tickers from excel (1 column with all the tickers) and retrieve the data from yfinance for each one of them, but sometimes it hangs on a run with no errors.

Here is the script:

import pandas as pd
import pandas_datareader.data as web
import yfinance as yf
yf.pdr_override()
import datetime

while 1>0:
    excel = pd.read_excel ('LIST.xlsx', header=None)
    tickerslist = excel.iloc[:, 0].astype(str)
    elementoslista = tickerslist.count()
    listadf = pd.DataFrame(tickerslist)
    listadf.columns = ['Tickers']
    print("INÍCIO:",datetime.datetime.now())
    datanow = datetime.datetime.now()
    datahora = datanow.hour
    datamin = datanow.minute
    dia = str(datetime.datetime.now().day)+"_"+str(datetime.datetime.now().month)
    horario = str(datetime.datetime.now().hour)+"_"+str(datetime.datetime.now().minute)
    for ticker in tickerslist:
        elementpos = listadf[listadf['Tickers'] == ticker].index.values
        tickeryf = yf.Ticker(ticker)
        print(tickeryf, elementpos," de ",elementoslista)
        df = web.get_data_yahoo(ticker, progress=False, show_errors=False, threads = False)
        dfday = web.get_data_yahoo(ticker,period='1d', interval='5m', progress=False, show_errors=False, threads = False)

The hanging always occurs while defining the dataframe (web.get_data_yahoo), tried changing directly for yahoo (yf.download) but the problem persists.

It is not related to one specific ticker, since sometimes it runs the whole list, sometimes it stops at position 10, sometimes position 20, and so on.

Tried it both in linux and windows, on command line and with an IDE (Pycharm), with high memory (16gb), tried using garbage collector (gc) since I thought the problem might occur because of tkinter, but none of those solved my problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source