Book Image

Learning Pandas

By : Michael Heydt
Book Image

Learning Pandas

By: Michael Heydt

Overview of this book

Table of Contents (19 chapters)
Learning pandas
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Obtaining and organizing stock data from Yahoo!


The first step we will take is to write a couple of functions that help us with retrieving stock data from Yahoo! Finance. We have already seen that this data can be read using a pandas DataReader object, but we will need to organize the data a little differently than how it is provided by Yahoo! as we are going to perform various pivots of this information later.

To facilitate this, we will start with the following function to get all the Yahoo! data for a specific stock between the two specified dates and also add the stock's symbol in a column for each entry. This will be needed later for pivots:

In [2]:
   # read data from Yahoo! Finance for a specific 
   # stock specified by ticker and between the start and end dates
   def getStockData(ticker, start, end):
       # read the data
       data = pd.io.data.DataReader(ticker, "yahoo", start, end)
       # rename this column
       data.rename(columns={'Adj Close': 'AdjClose'}, inplace=True...