What will we cover in this tutorial?
In this tutorial we will cover the following.
- How to use Pandas Datareader to read historical stock prices from Yahoo! Finance.
- Learn how to read weekly and monthly data.
- Also how to read multiple tickers at once.
Step 1: What is Pandas Datareader?
Pandas-Datareader is an up to date remote data access for pandas.
This leads to the next question. What is pandas?
Pandas is a data analysis and manipulation tool containing a great data structure for the purpose.
Shortly said, pandas can be thought of as a data structure in Python, which is similar to working with data in a spreadsheet.
Pandas-datareader reads data from various sources and puts the data into a pandas data structures.
Pandas-datareader has a call to return historic stock price data from Yahoo! Finance.
To use Pandas-datareader you need to import the library.
Step 2: Example reading data from Yahoo! Finance with Pandas-Datareader
Let’s break the following example down.
import pandas_datareader as pdr
import datetime as dt
ticker = "AAPL"
start = dt.datetime(2019, 1, 1)
end = dt.datetime(2020, 12, 31)
data = pdr.get_data_yahoo(ticker, start, end)
print(data)
Where we first import two libraries.
- pandas_datareader The Pandas Datareader. If you do not have it installed already in your Jupyter Notebook you can do that by entering this in a cell !pip install pandas_datareader and execute it.
- datetime This is a default library and represents a date and time. We only use it for the date aspects.
The the following lines.
- ticker = “AAPL” The ticker we want data from. You can use any ticker you want. In this course we have used the ticker for Apple (AAPL).
- start = dt.datetime(2019, 1, 1) Is the starting day we want historic stock price data.
- end = dt.datetime(2020, 12, 31) The end day.
- data = pdr.get_data_yahoo(ticker, start, end) This is the magic that uses Pandas Datareader (pdr) to get data from the Yahoo! Finance API. It returns a DataFrame as we know it from previous lessons.
The output of the code is as follows.
High Low ... Volume Adj Close
Date ...
2019-01-02 39.712502 38.557499 ... 148158800.0 38.505024
2019-01-03 36.430000 35.500000 ... 365248800.0 34.669640
2019-01-04 37.137501 35.950001 ... 234428400.0 36.149662
2019-01-07 37.207500 36.474998 ... 219111200.0 36.069202
2019-01-08 37.955002 37.130001 ... 164101200.0 36.756794
... ... ... ... ... ...
2020-12-24 133.460007 131.100006 ... 54930100.0 131.773087
2020-12-28 137.339996 133.509995 ... 124486200.0 136.486053
2020-12-29 138.789993 134.339996 ... 121047300.0 134.668762
2020-12-30 135.990005 133.399994 ... 96452100.0 133.520477
2020-12-31 134.740005 131.720001 ... 99116600.0 132.492020
[505 rows x 6 columns]
Step 3: A few parameters to set
You can get multiple tickers at once by parsing a list of them.
import pandas_datareader as pdr
import datetime as dt
ticker = ["AAPL", "IBM", "TSLA"]
start = dt.datetime(2019, 1, 1)
end = dt.datetime(2020, 12, 31)
data = pdr.get_data_yahoo(ticker, start, end)
print(data)
You can get the weekly or monthly data by using the argument as follows.
import datetime as dt
ticker = ["AAPL", "IBM", "TSLA"]
start = dt.datetime(2019, 1, 1)
end = dt.datetime(2020, 12, 31)
data = pdr.get_data_yahoo(ticker, start, end, interval='w')
print(data)
Set interval=’m’ to get monthly data instead of weekly with ‘w’.
Want to learn more?
This is part of the course of Master Technical Analysis with pandas.
In the next lesson you will learn how to Calculate the Volatility of Historic Stock Prices with Pandas and Python.
12% Investment Solution
Would you like to get 12% in return of your investments?
D. A. Carter promises and shows how his simple investment strategy will deliver that in the book The 12% Solution. The book shows how to test this statement by using backtesting.
Did Carter find a strategy that will consistently beat the market?
Actually, it is not that hard to use Python to validate his calculations. But we can do better than that. If you want to work smarter than traditional investors then continue to read here.
Python for Finance: Unlock Financial Freedom and Build Your Dream Life
Discover the key to financial freedom and secure your dream life with Python for Finance!
Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.
Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.
Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.
Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!
Python for Finance a 21 hours course that teaches investing with Python.
Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.
“Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

import pandas as pdr
import datetime as dt
Ticker = [“x”,”rol”,]
data = pdr.get_data_yahoo(ticker, start, end, interval=’w’)
print(data)
will not work I have tried everything
As you mention in later comment it should be: import pandas_datareader as pdr
Doesnt work…
====================
>>> ticker = “AAPL”
>>> start = dt.datetime(2019, 1, 1)
>>> end = dt.datetime(2020, 12, 31)
>>>
>>> data = pdr.get_data_yahoo(ticker, start, end)
Traceback (most recent call last):
File “”, line 1, in
File “/home/yoan/.local/lib/python3.10/site-packages/pandas_datareader/data.py”, line 80, in get_data_yahoo
return YahooDailyReader(*args, **kwargs).read()
File “/home/yoan/.local/lib/python3.10/site-packages/pandas_datareader/base.py”, line 253, in read
df = self._read_one_data(self.url, params=self._get_params(self.symbols))
File “/home/yoan/.local/lib/python3.10/site-packages/pandas_datareader/yahoo/daily.py”, line 153, in _read_one_data
data = j[“context”][“dispatcher”][“stores”][“HistoricalPriceStore”]
TypeError: string indices must be integers
Hi John,
There is a breaking change in the Yahoo data.
You can fix it like this: https://www.learnpythonwithrune.org/fix-get_data_yahoo-from-pandas-datareader/
Rune