Book Image

Mastering Pandas for Finance

By : Michael Heydt
Book Image

Mastering Pandas for Finance

By: Michael Heydt

Overview of this book

Table of Contents (16 chapters)
Mastering pandas for Finance
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Loading historical stock data


The examples in this chapter will utilize data extracted from Yahoo! Finance. This information can be extracted live from the web services or from files provided with the source. This data consists of stock prices for MSFT and AAPL for the year 2012.

The following command can be used to load the stock information directly from the Web:

In [2]:
   import pandas.io.data as web

   start = datetime.datetime(2012, 1, 1)
   end = datetime.datetime(2012, 12, 30)

   msft = web.DataReader("MSFT", 'yahoo', start, end)
   aapl = web.DataReader("AAPL", 'yahoo', start, end)

   # these save the data to file - optional for the examples
   #msft.to_csv("msft.csv")
   #aapl.to_csv("aapl.csv")

If you are not online or just want to load the data from the file, you can use the following command. I actually recommend using this data as even though the online data is historical, the adjusted close values are sometimes changed to represent other events and can potentially cause...