Code to get the fundamental data for the stocks - KeyError

Hi,

when I run this code to get the fundamental data for the S&P500 stocks:



 # Import libraries

import yfinance as yf
import pandas as pd
 
# List of all S&P500 tickers
tickers = pd.read_html(
    'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
 
# Variable 'stocks' to store the list of all ticker names
stocks = tickers.Symbol
 
# Uncomment the below line to test on a smaller list of stocks
#stocks = ["AAPL", "MSFT", "AMZN"]  # Feel free to add more
 
# Create a dataframe called 'data' with column stock
data = pd.DataFrame(columns=['Stock'])
 
# A loop that iterates through each ticker in the list passed earlier
# Here, we get the data for ticker i and store it in respective variables
for i in stocks:
    # Note: In the below code, we have first converted i to yf.Ticker object
    profitMargins = yf.Ticker(i).get_info()['profitMargins']
    revenueGrowth = yf.Ticker(i).get_info()['revenueGrowth']
    returnOnEquity = yf.Ticker(i).get_info()['returnOnEquity']
 
    # Print the ticker name and fundamental attributes at each iteration
    print(i, profitMargins, revenueGrowth, returnOnEquity)
 
    # Add the new data to the dataframe 'data'
    data = data.append({'Stock': i, 'profitMargins': profitMargins,
                       'revenueGrowth': revenueGrowth, 'returnOnEquity': returnOnEquity}, ignore_index=True)
 
# Display the dataframe

data



I read this error:

KeyError                                  Traceback (most recent call last)
<ipython-input-1-e503ae675393> in <module>
     20 for i in stocks:
     21     # Note: In the below code, we have first converted i to yf.Ticker object
---> 22     profitMargins = yf.Ticker(i).get_info()['profitMargins']
     23     revenueGrowth = yf.Ticker(i).get_info()['revenueGrowth']
     24     returnOnEquity = yf.Ticker(i).get_info()['returnOnEquity']

KeyError: 'profitMargins'

Hi Alessandro,

The yfinance package seems to have been updated today as shown here



Thus, in order to get the above code running, you can simply update the yfinance package to the latest version by running the following code:

!pip install yfinance==0.2.1

This should fix the issue.



Additionally, you may also come across some warning messages while running the code.

To ignore these, you can add the following lines of code:

​​​​​​​import warnings
warnings.filterwarnings('ignore')

​​​​​​​I hope this helps.

Hi,

I updated the yfinance package, but I have this KeyError, after some data downloaded:

 

KeyError                                  Traceback (most recent call last)
<ipython-input-2-e503ae675393> in <module>
     20 for i in stocks:
     21     # Note: In the below code, we have first converted i to yf.Ticker object
---> 22     profitMargins = yf.Ticker(i).get_info()['profitMargins']
     23     revenueGrowth = yf.Ticker(i).get_info()['revenueGrowth']
     24     returnOnEquity = yf.Ticker(i).get_info()['returnOnEquity']

KeyError: 'profitMargins'

Hi Alessandro,



The following is the link to my Jupyter notebook where the code seems to be running fine:

Download via yfinance.ipynb - Google Drive



I have also printed out the version of yfinance package that was installed on my system.



Do try using the same command in your notebook and check if the package was updated to a recent version. In case it hasn't been updated correctly, you can try uninstalling and reinstalling yfinance.



I hope this solves the problem.

 

Hi Kevin,

I use the code to extract tha data about all tickers of S&P500, but I have the KeyError after 65 tickers (WRB stock) every time that I run it.

Your example with a list of three stocks it's ok…

Hi Alessandro,

Thanks for the clarity.



You can use try and except statements to overcome this error.

Here's the link to the notebook with the updated code: Notebook



I hope this resolves the issue.

Now this is the error:

 

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-bf1c8a6257d6> in <module>
     39 
     40     # Print the ticker name and fundamental attributes at each iteration
---> 41     print(count, i, profitMargins, revenueGrowth, returnOnEquity)
     42 
     43     # Add the new data to the dataframe 'data'

NameError: name 'profitMargins' is not defined


Help me, please!

tks

Hi Alessandro,

Updating yfinance to the latest version - 0.2.4 should get the code running without any errors.

I have attached a screenshot for the same below.







If you are facing error in executing this on your local Jupyter notebook, you can try creating a fresh notebook on google collab and run the same code after installing the latest version of yfinance as shown above.



I hope this was helpful.

The code runs without errors now.

I have another question (I hope the last one!): how can I convert the "None" fundamental data above, to use them in Machine Learning algorithm and without delete the rows?

Thank you

 

Hi Alessandro,

Thats good to know!



Here are a few approaches you can try to convert the "None" fundamental data:

  • Try obtaining the data from a different source. And for this you can visit the course on Getting Market Data where we have mentioned plenty of free and paid data sources.
  • You could consider using historical value of the specific attribute and use the same for the current period.
  • Lastly, you could replace it with the attribute value of another asset that is highly similar in terms of industry sector, market cap and past performance.



    I hope this helped