Optimize Yahoo Finance Code for Analysis

I am trying to analyze a number of companies using financial data I gathered from Yahoo Finance. I am also using the yfinance API to get some more details about the company using functions. Since I am trying to do this for a number of companies Each Iteration needs to be quick. Currently, 1 Company takes about 3 seconds.

Is it because of the API calls or requests? Can I increase the below code efficiency?

import pandas as pd
import numpy as np
from pandas import ExcelWriter
import requests
import timeit
import yfinance as yf

def get_industry(stock):
    tickerdata = yf.Ticker(stock) 
    return tickerdata.info['industry']

def get_symbol(symbol):
    url = http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}region=1lang=en.format(symbol)
    result = requests.get(url).json()
    for x in result['ResultSet']['Result']:
        if x['symbol'] == symbol:
            return x['name']
def get_currency(stock):
    tickerdata = yf.Ticker(stock) 
    return tickerdata.info['currency']


quarters = ['Q4-2020','Q3-2020','Q2-2020','Q1-2020','Q4-2019','Q3-2019','Q2-2019','Q1-2019','Q4-2018','Q3-2018','Q2-2018','Q1-2018']
start_time = timeit.default_timer()
r = []
for i in stock[:1]:
    try:
        df = pd.read_csv(i+'_quarterly_balance-sheet.csv')
        df = df.drop('ttm',axis=1,errors='ignore')
        df['name'] = df['name'].str.replace('\t','')
        df = df.iloc[:,:13].T
        df.columns = df.iloc[0]
        df = df[1:]
        df.insert(0,'Q',quarters)
        try:
            df.insert(0,'Currency',get_currency(i))
        except:
            df.insert(0,'Currency','Not Found')
        df.insert(0,'Ticker',i)
        df.insert(0,'Company',get_symbol(i))
        try:
            df.insert(0,'Industry',get_industry(i))
        except:
            df.insert(0,'Industry','Unknown')
        df = df.loc[:,df.columns.isin(['Q','Currency','Ticker','Company','Industry','TotalRevenue'])]
        r.append(df)
    except:
        continue
df = pd.concat(r)
# code you want to evaluate
elapsed = timeit.default_timer() - start_time
elapsed

3.041297 Seconds

Topic finance optimization pandas data-cleaning data-mining

Category Data Science

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.