yfinance
yfinance is a popular open-source Python library designed to access and retrieve financial market data from Yahoo! Finance. It simplifies fetching historical prices, financial statements, dividends, splits, and other relevant information for stocks, indices, and securities. The library is actively maintained with frequent updates, enabling users to efficiently perform financial analyses and gain insights.
Warnings
- breaking As of version 1.2.0, the DataFrame returned by `yf.history()` is consolidated, which might cause 'read-only' errors in existing code if you attempt to modify it in place.
- breaking Around version 0.2.51 (December 2024), the data structure for downloaded data changed, often introducing a Multi-Index DataFrame. Additionally, the explicit 'Adjusted Close' column was removed as Open, High, Low, and Close prices are now automatically adjusted for dividends and splits.
- deprecated Version 1.0 introduced deprecation warnings for the old configuration method.
- gotcha yfinance is an unofficial library that scrapes data from Yahoo Finance. As such, its functionality is subject to breaking if Yahoo Finance changes its website layout or API endpoints. Issues like cookie reuse or DNS blocking have historically occurred.
- gotcha Intraday data (intervals less than 1 day) has significant limitations: 1-minute data is only available for the last 7 days, and any intraday interval data is only available for the last 60 days. End-of-day data typically has a much longer history.
Install
-
pip install yfinance
Imports
- yfinance
import yfinance as yf
Quickstart
import yfinance as yf
import pandas as pd
# Download historical data for a single ticker (e.g., Apple)
ticker_symbol = "AAPL"
data = yf.download(ticker_symbol, start="2023-01-01", end="2023-12-31")
print(f"Downloaded {len(data)} rows for {ticker_symbol}:")
print(data.head())
# Access information about a ticker
ticker = yf.Ticker(ticker_symbol)
info = ticker.info
print(f"\nMarket Cap for {ticker_symbol}: {info.get('marketCap')}")