Home > database >  Getting TTM income statement yahoo finance using yahoo_fin
Getting TTM income statement yahoo finance using yahoo_fin

Time:07-24

I try to get ttm values of the income statement for ticker symbol AAPL by using

from yahoo_fin import stock_info as si
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
import pandas_datareader

pd.set_option('display.max_columns', None)

income_statement = si.get_income_statement("aapl")
income_statement

but the result doesn't show the ttm values, only the yearly values are shown enter image description here

On yahoo finance, we can also see the ttm values:

enter image description here

Anyone who can help?

CodePudding user response:

The reason you don't get the exact same table you see on the website is because of the way yahoo_fin gets data from Yahoo. Rather than getting them from the table you see, they get them from json data that Yahoo provides. In this data, there are both quarterly and yearly income statements. When Yahoo renders the table on their website, they most likely use the yearly data for the yearly columns and then sum the last 4 quarterly results to get the TTM column (as TTM results are nothing else than the sum of the last 4 quarterly results).

If you want to get the TTM data, the best approach would be to do it the same way I assume Yahoo does. Get the quarterly data using yahoo_fin and then sum the quarters to calculate the TTM results. You can do this by setting the optional yearly parameter to False:

quarterly_income_statement = si.get_income_statement("aapl", yearly=False)

You can check out their method _parse_json to better understand how they get and parse data from Yahoo. (Assuming you have some knowledge of requests and json.)

Summing the data

To get the sum of the quarters you can for example do this:

quarterly_income_statement = si.get_income_statement("aapl", yearly=False)
ttm = quarterly_income_statement.sum(axis=1)

This will give you a new Dataframe ttm with the same data fields with values being TTM (You can test it and see if it matches the numbers on the website).

  • Related