How To Use yfinance Package

Install yfinance using pip as shown below. Make sure you also have Pandas installed.

In [1]:
!pip install yfinance --upgrade --no-cache-dir
In [2]:
import yfinance as yf
import pandas as pd
import json

yfinance has following methods.

isin
major_holders
institutional_holders
mutualfund_holders
dividends
splits
actions
shares
info
calendar
recommendations
earnings
quarterly_earnings
financials
quarterly_financials
balance_sheet
quarterly_balance_sheet
balancesheet
quarterly_balancesheet
cashflow
quarterly_cashflow
sustainability
options
news
analysis
earnings_history
earnings_dates

Let us try few of the above methods. To use above methods, we need to first get basic yfinance object.

In [3]:
gme = yf.Ticker("GME")

Now we can query for the data using any of the above methods. Let us get the earnings date for the GME stock using gme.earnings_dates method.

In [4]:
df = gme.earnings_dates

Let us check the data type.

In [5]:
type(df)
Out[5]:
pandas.core.frame.DataFrame

Since it is a dataframe, it is easy to query it.

In [6]:
gme.earnings_dates.head()
Out[6]:
EPS Estimate Reported EPS Surprise(%)
Earnings Date
2022-09-07 12:00:00-04:00 -0.38 -0.35 0.0741
2022-06-01 12:00:00-04:00 -0.36 -0.52 -0.4325
2022-03-17 12:00:00-04:00 0.21 -0.46 -3.2249
2021-12-08 11:00:00-05:00 -0.13 -0.35 -1.6977
2021-09-08 12:00:00-04:00 -0.17 -0.19 -0.1446

How about current price?

In [7]:
gme.info['currentPrice']
Out[7]:
28.64

To get historic data, use history() method. Let us query the historic data for 1 month.

In [8]:
gme.history(period="1mo").head(10)
Out[8]:
Open High Low Close Volume Dividends Stock Splits
Date
2022-08-17 42.180000 44.360001 40.410000 40.520000 9766400 0 0
2022-08-18 39.270000 40.070000 37.340000 37.930000 8145400 0 0
2022-08-19 35.180000 37.189999 34.669998 36.490002 9551900 0 0
2022-08-22 34.310001 36.200001 34.200001 34.500000 5798600 0 0
2022-08-23 34.700001 34.990002 33.450001 33.529999 4836300 0 0
2022-08-24 34.000000 34.939999 32.439999 32.500000 5620300 0 0
2022-08-25 32.840000 32.889999 31.500000 31.959999 4726300 0 0
2022-08-26 31.500000 32.380001 30.629999 30.940001 4297300 0 0
2022-08-29 30.480000 32.750000 30.379999 31.549999 4292700 0 0
2022-08-30 31.620001 31.870001 29.420000 29.840000 5060200 0 0

Let us try one more, the news method.

In [9]:
gme.news[0]
Out[9]:
{'uuid': '9dfaa4b2-dc7f-30dd-aaa2-439783561a0c',
 'title': 'GameStop: Cash burn, lack of profitability loom large over meme stock darling',
 'publisher': 'MarketWatch',
 'link': 'https://finance.yahoo.com/m/9dfaa4b2-dc7f-30dd-aaa2-439783561a0c/gamestop%3A-cash-burn%2C-lack-of.html',
 'providerPublishTime': 1663330860,
 'type': 'STORY',
 'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/TDSU9jYzPTpgFiMi7HRtJA--~B/aD02NDA7dz0xMjgwO2FwcGlkPXl0YWNoeW9u/https://s.yimg.com/uu/api/res/1.2/AeKNktRSByFU6KhwLciJLA--~B/aD02NDA7dz0xMjgwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/marketwatch.com/fa043a6bc00596e64afb00b7c5e83c65',
    'width': 1280,
    'height': 640,
    'tag': 'original'},
   {'url': 'https://s.yimg.com/uu/api/res/1.2/WCOFdAbfNHuWHTwH7kSafQ--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://s.yimg.com/uu/api/res/1.2/AeKNktRSByFU6KhwLciJLA--~B/aD02NDA7dz0xMjgwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/marketwatch.com/fa043a6bc00596e64afb00b7c5e83c65',
    'width': 140,
    'height': 140,
    'tag': '140x140'}]},
 'relatedTickers': ['GME', '^GSPC', 'APE']}