0

I am trying to analyze stocks within python using the api "yfinance." I get the program to run just fine most of the time. However, when it is unable to 'find' one of the fields, it throughs up an error (KeyError). Here is a snippet of my code.

import yfinance as yf
stock = yf.Ticker(stock)
pegRatio = stock.info['pegRatio']
print(pegRatio)
if pegRatio > 1:
    print("The PEG Ratio for "+name+" is high, indicating that it may be overvalued (based on projected earnings growth).")
if pegRatio == 1:
    print("The PEG Ratio for "+name+" is 1, indicating that it is close to fair value.")
if pegRatio < 1:
    print("The PEG Ratio for "+name+" is low, indicating that it may be undervalued (based on projected earnings growth).")

This was the error Traceback (most recent call last): line 29, in industry = stock.info['industry'] KeyError: 'industry'

My main question is how do I get the code to basically ignore the error and run the rest of the code?

3
  • 1
    Can you post the full error? Commented May 23, 2020 at 21:49
  • that's called "error handling" and you can find plenty of info about that online. Commented May 23, 2020 at 21:52
  • 1
    If you want to post code here do ``` all lines of your code here ``` Commented May 23, 2020 at 21:53

1 Answer 1

1

Error Handling in Python

Like Nicolas commented already, you will find many tutorials, blog posts and videos in the web. Also some basic Python books that cover "Error Handling".

The control-structure in Python consists of at least 2 keywords try and except, often named try-except block. There are also 2 optional keywords else and finally.

How to wrap your failing statements

import yfinance as yf

try: # st art watching for errors
  stock = yf.Ticker(stock)  # the API call may also fail, e.g. no connection
  pegRatio = stock.info['pegRatio']  # here your actual exception was thrown
except KeyError:  # catch unspecific or specific errors/exceptions
  # handling this error type begins here: print and return
  print "Error: Could not find PEG Ratio in Yahoo! Finance response!"
  return 

# the happy path continues here: with pegRatio
print(pegRatio)
if pegRatio > 1:
    print("The PEG Ratio for "+name+" is high, indicating that it may be overvalued (based on projected earnings growth).")
if pegRatio == 1:
    print("The PEG Ratio for "+name+" is 1, indicating that it is close to fair value.")
if pegRatio < 1:
    print("The PEG Ratio for "+name+" is low, indicating that it may be undervalued (based on projected earnings growth).")
Sign up to request clarification or add additional context in comments.

1 Comment

Raymond Hettinger suggests that it's more idiomatic to ignore ("suppress") expected errors and exceptions when it would otherwise just make the code more verbose and less readable. Use contextlib.suppress for that. See stackoverflow.com/a/15566001/202834

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.