0% found this document useful (0 votes)
4 views7 pages

yfinance · PyPI

yfinance is a Python library designed to download historical market data from Yahoo! Finance, addressing the issues caused by the decommissioning of Yahoo's historical data API. The latest version, 0.1.60, is compatible with Python 2.7 and 3.4+, and has gained significant popularity with around 224k monthly installs. It offers various features including access to stock information, historical data, dividends, and financials, and can be installed via pip or conda.

Uploaded by

daddpra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

yfinance · PyPI

yfinance is a Python library designed to download historical market data from Yahoo! Finance, addressing the issues caused by the decommissioning of Yahoo's historical data API. The latest version, 0.1.60, is compatible with Python 2.7 and 3.4+, and has gained significant popularity with around 224k monthly installs. It offers various features including access to stock information, historical data, dividends, and financials, and can be installed via pip or conda.

Uploaded by

daddpra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4/7/2021 yfinance · PyPI

Search projects 

Help Sponsors Log in Register

yfinance 0.1.60  Latest version

pip install yfinance  Released: about 7 hours


ago

Yahoo! Finance market data downloader

Navigation Project description


 Project
description Yahoo! Finance market data downloader
python 2.7, 3.4+ pypi v0.1.60 status stable installs 224k/month

build passing codefactor A Star 5.2k Follow 6.8k


 Release
history
Ever since Yahoo! finance decommissioned their historical data API,
many programs that relied on it to stop working.
 Download
files yfinance aims to solve this problem by offering a reliable, threaded,
and Pythonic way to download historical market data from Yahoo!
finance.

Project links
NOTE
 Homepage
The library was originally named fix-yahoo-finance , but I’ve
since renamed it to yfinance as I no longer consider it a mere “fix”.
Statistics For reasons of backward-compatibility, fix-yahoo-finance now
GitHub statistics: import and uses yfinance , but you should install and use
yfinance directly.
 Stars: 5.243
Changelog »
 Forks: 1.282

[Link] 1/7
4/7/2021 yfinance · PyPI

 Open ==> Check out this Blog post for a detailed tutorial with code
issues/PRs: 316 examples.

View statistics for this


project via
[Link] , or by Quick Start
using our public
dataset on Google
BigQuery 
The Ticker module

The Ticker module, which allows you to access ticker data in a


Meta more Pythonic way:

License: Apache Note: yahoo finance datetimes are received as UTC.


Software License
(Apache)
import yfinance as yf
Author: Ran Aroussi 

 pandas msft = [Link]("MSFT")

# get stock info


[Link]
Maintainers
# get historical market data
ranaroussi hist = [Link](period="max")

# show actions (dividends, splits)


[Link]
Classifiers
# show dividends
Development Status [Link]
5-
Production/Stable # show splits
[Link]
Intended Audience
Developers # show financials
[Link]
License msft.quarterly_financials
OSI Approved ::
Apache Software # show major holders
License msft.major_holders

Operating System # show institutional holders


OS Independent msft.institutional_holders

Programming # show balance sheet


Language msft.balance_sheet
Python :: 2.7 msft.quarterly_balance_sheet
Python :: 3.4
# show cashflow
Python :: 3.5
[Link] 2/7
4/7/2021 yfinance · PyPI

Python :: 3.6 [Link]


Python :: 3.7 msft.quarterly_cashflow
Python :: 3.8
# show earnings
Python :: 3.9
[Link]
Topic msft.quarterly_earnings

Office/Business ::
# show sustainability
Financial
[Link]
Office/Business ::
Financial :: # show analysts recommendations
Investment [Link]
Scientific/Engineering
:: Interface # show next event (earnings, etc)
Engine/Protocol [Link]
Translator
Software # show ISIN code - *experimental*
# ISIN = International Securities Identification Numbe
Development ::
[Link]
Libraries
Software
# show options expirations
Development :: [Link]
Libraries :: Python
Modules # get option chain for specific expiration
opt = msft.option_chain('YYYY-MM-DD')
# data available via: [Link], [Link]

If you want to use a proxy server for downloading data, use:

import yfinance as yf

msft = [Link]("MSFT")

[Link](..., proxy="PROXY_SERVER")
msft.get_actions(proxy="PROXY_SERVER")
msft.get_dividends(proxy="PROXY_SERVER")
msft.get_splits(proxy="PROXY_SERVER")
msft.get_balance_sheet(proxy="PROXY_SERVER")
msft.get_cashflow(proxy="PROXY_SERVER")
msft.option_chain(..., proxy="PROXY_SERVER")
...

To use a custom requests session (for example to cache calls to the


API or customize the User-agent header), pass a session=
argument to the Ticker constructor.

[Link] 3/7
4/7/2021 yfinance · PyPI

import requests_cache
session = requests_cache.CachedSession('[Link]
[Link]['User-agent'] = 'my-program/1.0'
ticker = [Link]('msft aapl goog', session=session)
# The scraped response will be stored in the cache
[Link]

To initialize multiple Ticker objects, use

import yfinance as yf

tickers = [Link]('msft aapl goog')


# ^ returns a named tuple of Ticker objects

# access each ticker using (example)


[Link]
[Link](period="1mo")
[Link]

Fetching data for multiple tickers

import yfinance as yf
data = [Link]("SPY AAPL", start="2017-01-01", end=

I’ve also added some options to make life easier :)

data = [Link]( # or pdr.get_data_yahoo(...


# tickers list or string as well
tickers = "SPY AAPL MSFT",

# use "period" instead of start/end


# valid periods: 1d,5d,1mo,3mo,6mo,1y,2y,5y,10y
# (optional, default is '1mo')
period = "ytd",

# fetch data by interval (including intraday i


# valid intervals: 1m,2m,5m,15m,30m,60m,90m,1h
# (optional, default is '1d')
interval = "1m",

# group by ticker (to access via data['SPY'])


# (optional, default is 'column')

[Link] 4/7
4/7/2021 yfinance · PyPI

group_by = 'ticker',

# adjust all OHLC automatically


# (optional, default is False)
auto_adjust = True,

# download pre/post regular market hours data


# (optional, default is False)
prepost = True,

# use threads for mass downloading? (True/False


# (optional, default is True)
threads = True,

# proxy URL scheme use use when downloading?


# (optional, default is None)
proxy = None
)

Managing Multi-Level Columns

The following answer on Stack Overflow is for How to deal with


multi-level column names downloaded with yfinance?

yfinance returns a [Link] with multi-level


column names, with a level for the ticker and a level for the
stock price data
The answer discusses:
How to correctly read the the multi-level columns after
saving the dataframe to a csv with
[Link].to_csv

How to download single or multiple tickers into a single


dataframe with single level column names and a ticker
column

pandas_datareader override

If your code uses pandas_datareader and you want to download


data faster, you can “hijack”
pandas_datareader.data.get_data_yahoo() method to use
yfinance while making sure the returned data is in the same format
as pandas_datareader’s get_data_yahoo() .

[Link] 5/7
4/7/2021 yfinance · PyPI

from pandas_datareader import data as pdr

import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)

# download dataframe
data = pdr.get_data_yahoo("SPY", start="2017-01-01", en

Installation

Install yfinance using pip :

$ pip install yfinance --upgrade --no-cache-dir

Install yfinance using conda :

$ conda install -c ranaroussi yfinance

Requirements

Python >= 2.7, 3.4+


Pandas (tested to work with >=0.23.1)
Numpy >= 1.11.1
requests >= 2.14.2
lxml >= 4.5.1

Optional (if you want to use pandas_datareader )

pandas_datareader >= 0.4.0

Legal Stuff

yfinance is distributed under the Apache Software License. See the


[Link] file in the release for details.

P.S.

[Link] 6/7
4/7/2021 yfinance · PyPI

Please drop me an note with any feedback you have.

Ran Aroussi

Help About PyPI

Installing packages  PyPI on Twitter 


Uploading packages  Infrastructure dashboard 
User guide  Package index name retention 
FAQs Our sponsors

Contributing to PyPI Using PyPI

Bugs and feedback Code of conduct 


Contribute on GitHub  Report security issue
Translate PyPI  Privacy policy 
Development credits  Terms of use

Status: All Systems Operational 

Developed and maintained by the Python community, for the Python community.
Donate today!

© 2021 Python Software Foundation 


Site map

Switch to desktop version

 English español français 日本語 português (Brasil) українська Ελληνικά Deutsch 中文 (简体)
中文 (繁體) русский ‫ עברית‬esperanto

[Link] 7/7

You might also like