Pandas_datareader for Yahoo stock prices

In other posts I have demonstrated how one can use quandl in Python to query time series data on e.g. equity prices. In this post I demonstrate how one can query stock price data from e.g. Yahoo finance, using the pandas_datareader module in Python.

In the example below I import pandas_datareader and query Procter & Gamble stock price data between 2020-01-01 and 2020-09-29:

# import relevant modules
import pandas_datareader.data as web
import datetime
# define datetimes for start and end dates
start_date = datetime.datetime(2020, 1, 1)
end_date = datetime.datetime(2020, 9, 29)
# import stock data for given period between start and end date form yahoo finance
df = web.DataReader("PG", "yahoo",start_date, end_date)
# display returned dataframe header
df.head()
HighLowOpenCloseVolumeAdj Close
Date
2020-01-02124.730003122.940002124.500000123.4100048130800.0121.104179
2020-01-03123.529999121.860001122.160004122.5800027970500.0120.289688
2020-01-06123.190002122.379997122.570000122.7500006674400.0120.456505
2020-01-07123.209999121.870003122.879997121.9899987583400.0119.710701
2020-01-08123.430000122.000000122.190002122.5100025385100.0120.220985

Using matplotlib.pyplot I can visualize the data by e.g. plotting daily closing prices. This is what I do in the following lines of code:

# import matplotlib.pyplot
import matplotlib.pyplot as plt
# create figure
plt.figure(figsize=(17.5,10))
# create line plot for closing prices
plt.plot(df.index,df["Close"],color="red")
# add title to plot
plt.title("Procter & Gamble daily stock closing prices (src: Yahoo)",size=22)
# add x-axis label
plt.xlabel("Date",size=16)
# add y-axis label
plt.ylabel("Closing price [USD]",size=16)
Text(0, 0.5, 'Closing price [USD]')

Below I another example, retrieving and visualizing stock price data going all the way back to 2015:

# query data again
df = web.DataReader("PG", "yahoo",datetime.datetime(2015,1,1), datetime.datetime(2020,9,29))
# create figure
plt.figure(figsize=(17.5,10))
# create line plot for closing prices
plt.plot(df.index,df["Close"],color="red")
# add title to plot
plt.title("Procter & Gamble daily stock closing prices (src: Yahoo)",size=22)
# add x-axis label
plt.xlabel("Date",size=16)
# add y-axis label
plt.ylabel("Closing price [USD]",size=16)
Text(0, 0.5, 'Closing price [USD]')

And finally another example going back all the way to year 2000:

# query data again
df = web.DataReader("PG", "yahoo",datetime.datetime(2000,1,1), datetime.datetime(2020,9,29))
# create figure
plt.figure(figsize=(17.5,10))
# create line plot for closing prices
plt.plot(df.index,df["Close"],color="red")
# add title to plot
plt.title("Procter & Gamble daily stock closing prices (src: Yahoo)",size=22)
# add x-axis label
plt.xlabel("Date",size=16)
# add y-axis label
plt.ylabel("Closing price [USD]",size=16)
Text(0, 0.5, 'Closing price [USD]')

You May Also Like

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.