The yfinance package — Tyson's Python Tutorial (2024)

GitHub: ranaroussi/yfinance

yfinance is a popular Python library that provides a straightforward way to download financial market data from Yahoo! Finance. This guide will introduce you to the basics of yfinance, including fetching data and utilizing key features.

Once installed, you can import yFinance into your Python script to start using its functionalities.

import yfinance as yf

The Ticker object#

The Ticker object is the primary way to access data from Yahoo! Finance. It represents a single stock or other financial instrument.When you create a Ticker object by passing a ticker symbol to the yf.Ticker() method, you get access to various attributes and methods that allow you to retrieve detailed information about that particular ticker.

Let’s create a Ticker object for Apple Inc. (ticker symbol: AAPL).

aapl = yf.Ticker("AAPL")

Exploring stock information#

yFinance provides various methods to access detailed information about a stock.

Company information#

Access a variety of metadata about the company, including sector, industry, and more.

# Get company informationaapl.info
{'address1': 'One Apple Park Way', 'city': 'Cupertino', 'state': 'CA', 'zip': '95014', 'country': 'United States', 'phone': '408 996 1010', 'website': 'https://www.apple.com', 'industry': 'Consumer Electronics', 'industryKey': 'consumer-electronics', 'industryDisp': 'Consumer Electronics', 'sector': 'Technology', 'sectorKey': 'technology', 'sectorDisp': 'Technology', 'longBusinessSummary': 'Apple Inc. designs, manufactures, and markets smartphones, personal computers, tablets, wearables, and accessories worldwide. The company offers iPhone, a line of smartphones; Mac, a line of personal computers; iPad, a line of multi-purpose tablets; and wearables, home, and accessories comprising AirPods, Apple TV, Apple Watch, Beats products, and HomePod. It also provides AppleCare support and cloud services; and operates various platforms, including the App Store that allow customers to discover and download applications and digital content, such as books, music, video, games, and podcasts. In addition, the company offers various services, such as Apple Arcade, a game subscription service; Apple Fitness+, a personalized fitness service; Apple Music, which offers users a curated listening experience with on-demand radio stations; Apple News+, a subscription news and magazine service; Apple TV+, which offers exclusive original content; Apple Card, a co-branded credit card; and Apple Pay, a cashless payment service, as well as licenses its intellectual property. The company serves consumers, and small and mid-sized businesses; and the education, enterprise, and government markets. It distributes third-party applications for its products through the App Store. The company also sells its products through its retail and online stores, and direct sales force; and third-party cellular network carriers, wholesalers, retailers, and resellers. Apple Inc. was founded in 1976 and is headquartered in Cupertino, California.', 'fullTimeEmployees': 150000, 'companyOfficers': [{'maxAge': 1, 'name': 'Mr. Timothy D. Cook', 'age': 62, 'title': 'CEO & Director', 'yearBorn': 1961, 'fiscalYear': 2023, 'totalPay': 16239562, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Luca Maestri', 'age': 60, 'title': 'CFO & Senior VP', 'yearBorn': 1963, 'fiscalYear': 2023, 'totalPay': 4612242, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Jeffrey E. Williams', 'age': 59, 'title': 'Chief Operating Officer', 'yearBorn': 1964, 'fiscalYear': 2023, 'totalPay': 4637585, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Ms. Katherine L. Adams', 'age': 59, 'title': 'Senior VP, General Counsel & Secretary', 'yearBorn': 1964, 'fiscalYear': 2023, 'totalPay': 4618064, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': "Ms. Deirdre O'Brien", 'age': 56, 'title': 'Senior Vice President of Retail', 'yearBorn': 1967, 'fiscalYear': 2023, 'totalPay': 4613369, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Chris Kondo', 'title': 'Senior Director of Corporate Accounting', 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. James Wilson', 'title': 'Chief Technology Officer', 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Suhasini Chandramouli', 'title': 'Director of Investor Relations', 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Greg Joswiak', 'title': 'Senior Vice President of Worldwide Marketing', 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}, {'maxAge': 1, 'name': 'Mr. Adrian Perica', 'age': 49, 'title': 'Head of Corporate Development', 'yearBorn': 1974, 'fiscalYear': 2023, 'exercisedValue': 0, 'unexercisedValue': 0}], 'auditRisk': 6, 'boardRisk': 1, 'compensationRisk': 2, 'shareHolderRightsRisk': 1, 'overallRisk': 1, 'governanceEpochDate': 1714521600, 'compensationAsOfEpochDate': 1703980800, 'irWebsite': 'http://investor.apple.com/', 'maxAge': 86400, 'priceHint': 2, 'previousClose': 189.84, 'open': 189.38, 'dayLow': 189.22, 'dayHigh': 190.81, 'regularMarketPreviousClose': 189.84, 'regularMarketOpen': 189.38, 'regularMarketDayLow': 189.22, 'regularMarketDayHigh': 190.81, 'dividendRate': 1.0, 'dividendYield': 0.0053, 'exDividendDate': 1715299200, 'payoutRatio': 0.14930001, 'fiveYearAvgDividendYield': 0.73, 'beta': 1.264, 'trailingPE': 29.482918, 'forwardPE': 26.26141, 'volume': 40771639, 'regularMarketVolume': 40771639, 'averageVolume': 64275736, 'averageVolume10days': 58961950, 'averageDailyVolume10Day': 58961950, 'bid': 189.81, 'ask': 189.91, 'bidSize': 100, 'askSize': 100, 'marketCap': 2911485558784, 'fiftyTwoWeekLow': 164.08, 'fiftyTwoWeekHigh': 199.62, 'priceToSalesTrailing12Months': 7.629219, 'fiftyDayAverage': 174.111, 'twoHundredDayAverage': 180.77705, 'trailingAnnualDividendRate': 0.96, 'trailingAnnualDividendYield': 0.00505689, 'currency': 'USD', 'enterpriseValue': 2948922081280, 'profitMargins': 0.26306, 'floatShares': 15308474083, 'sharesOutstanding': 15334099968, 'sharesShort': 94308265, 'sharesShortPriorMonth': 108782648, 'sharesShortPreviousMonthDate': 1711584000, 'dateShortInterest': 1714435200, 'sharesPercentSharesOut': 0.0062, 'heldPercentInsiders': 0.052199997, 'heldPercentInstitutions': 0.57553, 'shortRatio': 1.66, 'shortPercentOfFloat': 0.0062, 'impliedSharesOutstanding': 15467299840, 'bookValue': 4.837, 'priceToBook': 39.25367, 'lastFiscalYearEnd': 1696032000, 'nextFiscalYearEnd': 1727654400, 'mostRecentQuarter': 1711756800, 'earningsQuarterlyGrowth': -0.022, 'netIncomeToCommon': 100389003264, 'trailingEps': 6.44, 'forwardEps': 7.23, 'pegRatio': 2.62, 'lastSplitFactor': '4:1', 'lastSplitDate': 1598832000, 'enterpriseToRevenue': 7.727, 'enterpriseToEbitda': 22.749, '52WeekChange': 0.08995402, 'SandP52WeekChange': 0.26490295, 'lastDividendValue': 0.25, 'lastDividendDate': 1715299200, 'exchange': 'NMS', 'quoteType': 'EQUITY', 'symbol': 'AAPL', 'underlyingSymbol': 'AAPL', 'shortName': 'Apple Inc.', 'longName': 'Apple Inc.', 'firstTradeDateEpochUtc': 345479400, 'timeZoneFullName': 'America/New_York', 'timeZoneShortName': 'EDT', 'uuid': '8b10e4ae-9eeb-3684-921a-9ab27e4d87aa', 'messageBoardId': 'finmb_24937', 'gmtOffSetMilliseconds': -14400000, 'currentPrice': 189.87, 'targetHighPrice': 250.0, 'targetLowPrice': 164.0, 'targetMeanPrice': 202.26, 'targetMedianPrice': 200.0, 'recommendationMean': 2.1, 'recommendationKey': 'buy', 'numberOfAnalystOpinions': 39, 'totalCash': 67150000128, 'totalCashPerShare': 4.379, 'ebitda': 129629003776, 'totalDebt': 104590000128, 'quickRatio': 0.875, 'currentRatio': 1.037, 'totalRevenue': 381623009280, 'debtToEquity': 140.968, 'revenuePerShare': 24.537, 'returnOnAssets': 0.22073999, 'returnOnEquity': 1.4725, 'freeCashflow': 84726874112, 'operatingCashflow': 110563000320, 'earningsGrowth': 0.007, 'revenueGrowth': -0.043, 'grossMargins': 0.45586, 'ebitdaMargins': 0.33968, 'operatingMargins': 0.30743, 'financialCurrency': 'USD', 'trailingPegRatio': 2.2515}

Note: The information returned by info is a dictionary, and a lengthy one at that. In order to access each piece of information, you can use the dictionary’s key-value pairs. For example, to access the company’s sector, you would use aapl.info['sector'].

aapl.info['sector']
'Technology'

Stock fundamentals#

Retrieve financials and dividend history.

# Show financialsaapl.financials
2023-09-30 2022-09-30 2021-09-30 2020-09-30
Tax Effect Of Unusual Items 0.0 0.0 0.0 0.0
Tax Rate For Calcs 0.147 0.162 0.133 0.144
Normalized EBITDA 129188000000.0 133138000000.0 123136000000.0 81020000000.0
Net Income From Continuing Operation Net Minority Interest 96995000000.0 99803000000.0 94680000000.0 57411000000.0
Reconciled Depreciation 11519000000.0 11104000000.0 11284000000.0 11056000000.0
Reconciled Cost Of Revenue 214137000000.0 223546000000.0 212981000000.0 169559000000.0
EBITDA 129188000000.0 133138000000.0 123136000000.0 81020000000.0
EBIT 117669000000.0 122034000000.0 111852000000.0 69964000000.0
Net Interest Income -183000000.0 -106000000.0 198000000.0 890000000.0
Interest Expense 3933000000.0 2931000000.0 2645000000.0 2873000000.0
Interest Income 3750000000.0 2825000000.0 2843000000.0 3763000000.0
Normalized Income 96995000000.0 99803000000.0 94680000000.0 57411000000.0
Net Income From Continuing And Discontinued Operation 96995000000.0 99803000000.0 94680000000.0 57411000000.0
Total Expenses 268984000000.0 274891000000.0 256868000000.0 208227000000.0
Total Operating Income As Reported 114301000000.0 119437000000.0 108949000000.0 66288000000.0
Diluted Average Shares 15812547000.0 16325819000.0 16864919000.0 17528214000.0
Basic Average Shares 15744231000.0 16215963000.0 16701272000.0 17352119000.0
Diluted EPS 6.13 6.11 5.61 3.28
Basic EPS 6.16 6.15 5.67 3.31
Diluted NI Availto Com Stockholders 96995000000.0 99803000000.0 94680000000.0 57411000000.0
Net Income Common Stockholders 96995000000.0 99803000000.0 94680000000.0 57411000000.0
Net Income 96995000000.0 99803000000.0 94680000000.0 57411000000.0
Net Income Including Noncontrolling Interests 96995000000.0 99803000000.0 94680000000.0 57411000000.0
Net Income Continuous Operations 96995000000.0 99803000000.0 94680000000.0 57411000000.0
Tax Provision 16741000000.0 19300000000.0 14527000000.0 9680000000.0
Pretax Income 113736000000.0 119103000000.0 109207000000.0 67091000000.0
Other Income Expense -382000000.0 -228000000.0 60000000.0 -87000000.0
Other Non Operating Income Expenses -382000000.0 -228000000.0 60000000.0 -87000000.0
Net Non Operating Interest Income Expense -183000000.0 -106000000.0 198000000.0 890000000.0
Interest Expense Non Operating 3933000000.0 2931000000.0 2645000000.0 2873000000.0
Interest Income Non Operating 3750000000.0 2825000000.0 2843000000.0 3763000000.0
Operating Income 114301000000.0 119437000000.0 108949000000.0 66288000000.0
Operating Expense 54847000000.0 51345000000.0 43887000000.0 38668000000.0
Research And Development 29915000000.0 26251000000.0 21914000000.0 18752000000.0
Selling General And Administration 24932000000.0 25094000000.0 21973000000.0 19916000000.0
Gross Profit 169148000000.0 170782000000.0 152836000000.0 104956000000.0
Cost Of Revenue 214137000000.0 223546000000.0 212981000000.0 169559000000.0
Total Revenue 383285000000.0 394328000000.0 365817000000.0 274515000000.0
Operating Revenue 383285000000.0 394328000000.0 365817000000.0 274515000000.0
# Show dividendsaapl.dividends
Date1987-05-11 00:00:00-04:00 0.0005361987-08-10 00:00:00-04:00 0.0005361987-11-17 00:00:00-05:00 0.0007141988-02-12 00:00:00-05:00 0.0007141988-05-16 00:00:00-04:00 0.000714 ... 2023-05-12 00:00:00-04:00 0.2400002023-08-11 00:00:00-04:00 0.2400002023-11-10 00:00:00-05:00 0.2400002024-02-09 00:00:00-05:00 0.2400002024-05-10 00:00:00-04:00 0.250000Name: Dividends, Length: 83, dtype: float64

Fetching historical stock prices#

yFinance makes it simple to download historical market data available on Yahoo! Finance.

Basic data download#

Fetch the historical market data for Apple Inc.

# Get historical market datahist = aapl.history(period="max")hist
Open High Low Close Volume Dividends Stock Splits
Date
1980-12-12 00:00:00-05:00 0.099058 0.099488 0.099058 0.099058 469033600 0.0 0.0
1980-12-15 00:00:00-05:00 0.094320 0.094320 0.093890 0.093890 175884800 0.0 0.0
1980-12-16 00:00:00-05:00 0.087429 0.087429 0.086999 0.086999 105728000 0.0 0.0
1980-12-17 00:00:00-05:00 0.089152 0.089582 0.089152 0.089152 86441600 0.0 0.0
1980-12-18 00:00:00-05:00 0.091737 0.092167 0.091737 0.091737 73449600 0.0 0.0
... ... ... ... ... ... ... ...
2024-05-13 00:00:00-04:00 185.440002 187.100006 184.619995 186.279999 72044800 0.0 0.0
2024-05-14 00:00:00-04:00 187.509995 188.300003 186.289993 187.429993 52393600 0.0 0.0
2024-05-15 00:00:00-04:00 187.910004 190.649994 187.369995 189.720001 70400000 0.0 0.0
2024-05-16 00:00:00-04:00 190.470001 191.100006 189.660004 189.839996 52845200 0.0 0.0
2024-05-17 00:00:00-04:00 189.509995 190.809998 189.179993 189.869995 41260800 0.0 0.0

10949 rows × 7 columns

Customizing data retrieval#

You can specify the time period and data interval (e.g., daily, weekly).

# Fetch data for the last 5 years at a monthly intervalhist = aapl.history(period="5y", interval="1mo")hist
Open High Low Close Volume Dividends Stock Splits
Date
2019-06-01 00:00:00-04:00 42.367776 48.633671 41.081783 47.753017 2060874800 0.0000 0.0
2019-07-01 00:00:00-04:00 49.019694 53.410885 47.871230 51.401070 1895406800 0.0000 0.0
2019-08-01 00:00:00-04:00 51.608574 52.605038 46.464608 50.363602 2724326400 0.1925 0.0
2019-09-01 00:00:00-04:00 49.995487 54.836886 49.460247 54.243519 2170268400 0.0000 0.0
2019-10-01 00:00:00-04:00 54.509927 60.487199 52.102548 60.247429 2433210800 0.0000 0.0
2019-11-01 00:00:00-04:00 60.436336 64.907183 60.344306 64.725540 1793326000 0.1925 0.0
2019-12-01 00:00:00-05:00 64.924727 71.410644 62.257491 71.332909 2388794800 0.0000 0.0
2020-01-01 00:00:00-05:00 71.962072 79.640718 71.114290 75.185600 2934370400 0.0000 0.0
2020-02-01 00:00:00-05:00 73.919978 79.487664 62.276915 66.404091 3019279200 0.1925 0.0
2020-03-01 00:00:00-05:00 68.733672 74.022376 51.769399 61.918255 6280072400 0.0000 0.0
2020-04-01 00:00:00-04:00 60.021426 71.716473 57.683876 71.538719 3265299200 0.0000 0.0
2020-05-01 00:00:00-04:00 69.700341 78.950700 69.602945 77.416687 2805936000 0.2050 0.0
2020-06-01 00:00:00-04:00 77.579866 90.917989 77.448021 89.067299 3243375600 0.0000 0.0
2020-07-01 00:00:00-04:00 89.145419 103.926490 87.060344 103.775116 3020283200 0.0000 0.0
2020-08-01 00:00:00-04:00 105.669759 127.936587 105.369454 126.022415 4070061100 0.2050 4.0
2020-09-01 00:00:00-04:00 129.889196 134.996320 100.870567 113.305725 3885245100 0.0000 0.0
2020-10-01 00:00:00-04:00 115.096136 122.678548 105.390649 106.505997 2894666500 0.0000 0.0
2020-11-01 00:00:00-04:00 106.750596 119.352075 104.999302 116.475655 2123077300 0.2050 0.0
2020-12-01 00:00:00-05:00 118.597545 136.023074 117.617481 130.044693 2322189600 0.0000 0.0
2021-01-01 00:00:00-05:00 130.858136 142.197468 123.860473 129.329239 2240262000 0.0000 0.0
2021-02-01 00:00:00-05:00 131.083557 135.131226 116.029774 118.842560 1833855600 0.2050 0.0
2021-03-01 00:00:00-05:00 121.464151 126.342349 114.063426 119.893707 2650418200 0.0000 0.0
2021-04-01 00:00:00-04:00 121.375799 134.538098 120.227405 129.031723 1889857500 0.0000 0.0
2021-05-01 00:00:00-04:00 129.600999 131.593515 119.991843 122.308250 1711934900 0.2200 0.0
2021-06-01 00:00:00-04:00 122.978137 135.100943 121.060901 134.658508 1606590000 0.0000 0.0
2021-07-01 00:00:00-04:00 134.304577 147.479397 133.478681 143.408966 1919035100 0.0000 0.0
2021-08-01 00:00:00-04:00 143.900514 150.910703 142.071769 149.278595 1461542800 0.2200 0.0
2021-09-01 00:00:00-04:00 150.486943 154.849018 139.104173 139.330643 1797835100 0.0000 0.0
2021-10-01 00:00:00-04:00 139.724495 150.821716 136.150158 147.503387 1565079200 0.0000 0.0
2021-11-01 00:00:00-04:00 146.705830 163.159639 145.218970 162.765778 1691029000 0.2200 0.0
2021-12-01 00:00:00-05:00 165.152995 179.599454 155.607498 175.102814 2444766700 0.0000 0.0
2022-01-01 00:00:00-05:00 175.359188 180.398189 152.550557 172.351562 2108446000 0.0000 0.0
2022-02-01 00:00:00-05:00 171.592287 174.195606 149.888101 162.825806 1627516300 0.2200 0.0
2022-03-01 00:00:00-05:00 162.618571 177.340147 148.203090 172.403336 2180800100 0.0000 0.0
2022-04-01 00:00:00-04:00 171.830636 176.234277 153.416337 155.657639 1687795600 0.0000 0.0
2022-05-01 00:00:00-04:00 154.729535 164.376053 130.934100 146.958984 2401040300 0.2300 0.0
2022-06-01 00:00:00-04:00 148.223037 150.042464 127.596400 135.190491 1749099800 0.0000 0.0
2022-07-01 00:00:00-04:00 134.518106 161.799467 134.142368 160.691986 1447125400 0.0000 0.0
2022-08-01 00:00:00-04:00 159.208729 174.179353 155.382029 155.461136 1510239600 0.2300 0.0
2022-09-01 00:00:00-04:00 155.102806 162.648022 136.645731 136.843765 2084722800 0.0000 0.0
2022-10-01 00:00:00-04:00 136.853693 155.954385 133.051365 151.835205 1868139700 0.0000 0.0
2022-11-01 00:00:00-04:00 153.558087 153.924451 133.061234 146.577271 1724847700 0.2300 0.0
2022-12-01 00:00:00-05:00 146.998962 149.686810 124.841501 128.868317 1675731200 0.0000 0.0
2023-01-01 00:00:00-05:00 129.215474 146.026972 123.155399 143.110992 1443652500 0.0000 0.0
2023-02-01 00:00:00-05:00 142.793611 156.094041 140.165271 146.205505 1307198900 0.2300 0.0
2023-03-01 00:00:00-05:00 145.852601 163.901648 142.942097 163.802307 1520266600 0.0000 0.0
2023-04-01 00:00:00-04:00 163.176501 168.719358 158.716384 168.550476 969709700 0.0000 0.0
2023-05-01 00:00:00-04:00 168.153166 178.156141 163.216248 176.070114 1275155500 0.2400 0.0
2023-06-01 00:00:00-04:00 176.761258 193.452613 175.995322 192.945312 1297101100 0.0000 0.0
2023-07-01 00:00:00-04:00 192.756323 197.182812 185.614260 195.412216 996066400 0.0000 0.0
2023-08-01 00:00:00-04:00 195.203327 195.690729 171.051592 186.877533 1322439400 0.2400 0.0
2023-09-01 00:00:00-04:00 188.743524 189.231584 166.959669 170.535538 1337586600 0.0000 0.0
2023-10-01 00:00:00-04:00 170.545499 181.621688 165.017360 170.097275 1172719600 0.0000 0.0
2023-11-01 00:00:00-04:00 170.326347 192.169947 169.449809 189.201691 1099586100 0.2400 0.0
2023-12-01 00:00:00-05:00 189.829968 199.095555 186.957529 192.024185 1062774800 0.0000 0.0
2024-01-01 00:00:00-05:00 186.658318 195.864080 179.696660 183.915543 1187219300 0.0000 0.0
2024-02-01 00:00:00-05:00 183.506625 190.548074 178.779072 180.275131 1161627000 0.2400 0.0
2024-03-01 00:00:00-05:00 179.306803 180.285471 168.261786 171.247726 1432782800 0.0000 0.0
2024-04-01 00:00:00-04:00 170.958125 178.118411 163.857755 170.099289 1245717000 0.0000 0.0
2024-05-01 00:00:00-04:00 169.580002 191.100006 169.110001 189.869995 938213239 0.2500 0.0
The yfinance package — Tyson's Python Tutorial (2024)
Top Articles
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 6161

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.