How to get stock earnings data with Python

In this post, we’ll walk through a few examples for getting stock earnings data with Python. We will be using yahoo_fin, which was recently updated. The latest version now includes functionality to easily pull earnings calendar information for individual stocks or dates.

If you need to install yahoo_fin, you can use pip:

pip install yahoo_fin

If you already have it installed and need to upgrade, you can update your version like this:

pip install yahoo_fin –upgrade

To get started, let’s import yahoo_fin:

import yahoo_fin.stock_info as si

Getting stock earnings calendar data

The first method we’ll cover is the get_earnings_history function. get_earnings_history returns a list of dictionaries. Each dictionary contains an earnings date along with EPS actual / expected information. Let’s test it out with a few sample tickers.

# get earnings history for AAPL
aapl_earnings_hist = si.get_earnings_history(“aapl”)

Depending on the ticker, the data returned may include future earnings dates. Any data for future earnings dates will have a None value for EPS actual. Below, we look at one of the entries returned. We can see the earnings date for this entry is July 30, 2019. The EPS actual is 0.55 and the EPS estimate is 0.53.

If you want to convert the list of earnings dictionaries to a data frame, you can use pandas like this:

import pandas as pd

frame <- pd.DataFrame.from_dict(aapl_earnings_hist)

How to get the next earnings date for a stock

yahoo_fin also now has a function to find the next upcoming earnings date for an input stock ticker. If the next earnings date is known, then you can find it using the get_next_earnings_date method.

si.get_next_earnings_date(“aapl”)

si.get_next_earnings_date(“nflx”)

How to get stocks with earnings on a specific date

What if you want to know all the stocks that have earnings on a specific date? You can find them using the get_earnings_for_date method. This function takes a single date as input and returns a list of dictionaries. Each dictionary contains a ticker, company name, EPS estimate, and (if known) EPS actual. The EPS actual value will only have a value if you input a date in the past – otherwise, you’ll get a None type. get_earnings_for_date can handle a variety of date formats as input.

si.get_earnings_for_date(“02/18/2021”)

si.get_earnings_for_date(“2021-02-25”)

si.get_earnings_for_date(“March 1 2021”)

How to get stocks with earnings on a date range

Extending from the above section, you can also get all the stocks that have earnings within an input date range. To do that, let’s take a look at the get_earninggs_in_date_range method. This function takes a start date and end date as parameters.

earnings_in_week = si.get_earnings_in_date_range(“02/16/2021”, “02/23/2021”)

If you’re pulling a longer range, this method may take more time as the API needs to pull each day separately.

Getting recent revenue data

The last function we’ll cover in the article is the get_earnings method. get_earnings takes an input ticker and returns a dictionary of yearly / quarterly revenue and the last four quarters of EPS actual / estimate data (see here: https://finance.yahoo.com/quote/aapl/financials?p=aapl).

si.get_earnings(“aapl”)

si.get_earnings(“amzn”)

Conclusion

That’s it for now. In this post we covered how to get stock earnings data with Python and yahoo_fin. Check out more about yahoo_fin by clicking here. If you liked this post, please share it with your friends.

The post How to get stock earnings data with Python appeared first on Open Source Automation.

Leave a Reply

Your email address will not be published. Required fields are marked *