Skip to content

#315: Access Stock Market Data With yfinance

The stock market is a treasure trove for data analytics. The only problem: getting the data in a usable format without paying a fortune. Thanks to the work of Ran Aroussi and an active community, we can use a tool like yfinance to do the heavy lifting. Let us explore how we can use this tool to get the data we need.

Installation

With yfinance we can access the financial data (stocks, currencies, commodities, and more) from Yahoo! Finance. It wraps Yahoo Finance’s publicly available endpoints and exposes them through a clean, pythonic interface. Most importantly, it integrates seamlessly with Pandas, what makes it an ideal part in data-driven workflows.

Before we can access the data, we need to install it with this command:

uv pip install yfinance

Find the ticker symbol

To work with yfinance we need to know the name of the stock or commodity. The simplest way to figure this out is to go to finance.yahoo.com and search for the company name, currency or index. Here are a few examples for ticker symbols:

Name Symbol
NVIDIA Corporation NVDA
Apple Inc. AAPL
Bitcoin (in USD) BTC-USD
iShares Silver Trust SLV
Dow Jones Industrial Average ^DJI

Fetch the historical stock data

To get the historical stock data, we can use this code snipped and enter the symbol name for our stock:

1
2
3
4
5
6
7
import yfinance as yf

nvidia = yf.Ticker("NVDA")
df = nvidia.history("5d")

print(df.info())
print(df)

This gives us a Pandas DataFrame with 7 columns and a row for each day:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 5 entries, 2026-01-12 00:00:00-05:00 to 2026-01-16 00:00:00-05:00
Data columns (total 7 columns):
 #   Column        Non-Null Count  Dtype
---  ------        --------------  -----
 0   Open          5 non-null      float64
 1   High          5 non-null      float64
 2   Low           5 non-null      float64
 3   Close         5 non-null      float64
 4   Volume        5 non-null      int64
 5   Dividends     5 non-null      float64
 6   Stock Splits  5 non-null      float64
dtypes: float64(6), int64(1)
memory usage: 320.0 bytes
                                 Open        High         Low       Close     Volume  Dividends  Stock Splits
Date
2026-01-12 00:00:00-05:00  183.220001  187.119995  183.020004  184.940002  137968500        0.0           0.0
2026-01-13 00:00:00-05:00  185.000000  188.110001  183.399994  185.809998  160128900        0.0           0.0
2026-01-14 00:00:00-05:00  184.320007  184.460007  180.800003  183.139999  159586100        0.0           0.0
2026-01-15 00:00:00-05:00  186.500000  189.699997  186.330002  187.050003  206188600        0.0           0.0
2026-01-16 00:00:00-05:00  188.987503  190.440002  187.029999  187.119995  137897082        0.0           0.0

We selected the last week of trading (5d), but there are more options on how much days we get back:

1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max

Company information and fundamentals

We can access a detailed set of company related information through the info field. Depending on availability, this may include:

  • Market capitalization
  • Sector and industry
  • Business summary
  • Key financial ratios

The info field is a dictionary where we can access the data we want through a key or print everything that is returned from Yahoo:

import yfinance as yf
import pprint

nvidia = yf.Ticker("NVDA")

pprint.pp(nvidia.info["shortName"])
pprint.pp(nvidia.info)
'NVIDIA Corporation'

{'address1': '2788 San Tomas Expressway',
 'city': 'Santa Clara',
 'state': 'CA',
 'zip': '95051',
 'country': 'United States',
 'phone': '408 486 2000',
 'website': 'https://www.nvidia.com',
 'industry': 'Semiconductors',
 'industryKey': 'semiconductors',
 'industryDisp': 'Semiconductors',
 'sector': 'Technology',
 'sectorKey': 'technology',
 'sectorDisp': 'Technology',
...
 'marketCap': 4566401679360,
 'fiftyTwoWeekLow': 86.62,
 'fiftyTwoWeekHigh': 212.19,
 'allTimeHigh': 212.19,
 'allTimeLow': 0.033333,
...
 'totalCash': 60608000000,
 'totalCashPerShare': 2.494,
 'ebitda': 112696000512,
 'totalDebt': 10821999616,
 'quickRatio': 3.605,
 'currentRatio': 4.468,
 'totalRevenue': 187141996544,
 'debtToEquity': 9.102,
 'revenuePerShare': 7.668,
 'returnOnAssets': 0.53528,
 'returnOnEquity': 1.07359,
 'grossProfits': 131092996096,
 'freeCashflow': 53282873344,
 'operatingCashflow': 83158999040,
 'earningsGrowth': 0.667,
 'revenueGrowth': 0.625,
 'grossMargins': 0.7005,
 'ebitdaMargins': 0.60220003,
 'operatingMargins': 0.63168997,
 'financialCurrency': 'USD',
 'symbol': 'NVDA',
 'language': 'en-US',
 'region': 'US',
 'typeDisp': 'Equity',
 'quoteSourceName': 'Nasdaq Real Time Price',
 'customPriceAlertConfidence': 'HIGH',
 'shortName': 'NVIDIA Corporation',
}

This in only a fraction of the data we get for Nvidia. Take a close look at all the data in the full result set if you are interested in something special. If it is financial relevant, there is a high chance that Yahoo tracks it with their API.

If you want to know about upcoming events, check the calendar field:

pprint.pp(nvidia.calendar)
{'Dividend Date': datetime.date(2025, 12, 26),
 'Ex-Dividend Date': datetime.date(2025, 12, 4),
 'Earnings Date': [datetime.date(2026, 2, 25)],
 'Earnings High': 1.59,
 'Earnings Low': 1.49,
 'Earnings Average': 1.52204,
 'Revenue High': 68755000000,
 'Revenue Low': 62306000000,
 'Revenue Average': 65470015430}

Fetch multiple stocks at once

If we want to compare stocks, we can use a different approach and load them all together. To get the historical prices for Apple, Google, Microsoft, and Nvidia we can use this code:

import yfinance as yf

start = "2024-01-01"
end = "2025-12-31"
stocks = ["AAPL", "GOOG", "MSFT","NVDA"]

historic_prices = yf.Tickers(stocks).history(start=start, end=end)
print(historic_prices)

This gives us a DataFrame with 2 header levels and all the data we requested:

Price            Close                                     Dividends  ... Stock Splits    Volume                        
Ticker            AAPL        GOOG        MSFT        NVDA      AAPL  ...         NVDA      AAPL      GOOG      MSFT       NVDA
Date                                                                  ...                                               
2024-01-02  183.903229  138.521057  365.421600   48.141178       0.0  ...          0.0  82488700  20071900  25258600  411254000
2024-01-03  182.526215  139.315109  365.155548   47.542511       0.0  ...          0.0  58414500  18974300  23083500  320896000
2024-01-04  180.208115  137.012390  362.534637   47.971279       0.0  ...          0.0  71983600  18253300  20901500  306535000
2024-01-05  179.484970  136.367203  362.347443   49.069660       0.0  ...          0.0  62379700  15439500  21004600  415039000
2024-01-08  183.823944  139.483856  369.185516   52.223907       0.0  ...          0.0  59144500  17645300  23134000  642510000
...                ...         ...         ...         ...       ...  ...          ...       ...       ...       ...        ...
2025-12-23  272.359985  315.679993  486.850006  189.210007       0.0  ...          0.0  29642000  13961400  14683600  174873600
2025-12-24  273.809998  315.670013  488.019989  188.610001       0.0  ...          0.0  17910600   6138200   5855900   65528500
2025-12-26  273.399994  314.959991  487.709991  190.529999       0.0  ...          0.0  21521800   6730900   8842200  139740300
2025-12-29  273.760010  314.390015  487.100006  188.220001       0.0  ...          0.0  23715200  12317700  10893400  120006100
2025-12-30  273.079987  314.549988  487.480011  187.539993       0.0  ...          0.0  22139600  11052500  13944500   97687300

[501 rows x 28 columns]

Limitations

The data we get with yfinance may contain errors, could be delayed or not available. Therefore, do not use this library when you need to build real-time algorithmic trading systems or depend heavily on availability. For such use-cases you need a paid service with a defined service level.

Next

With yfinance it is easy to get the company information and the historical prices. But this is only the first step. Before we do any calculations, it is a good idea to visualise the data to spot errors and inconsistencies. Next week we explore a library that is made for exactly this task.