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 ...

Get Mastering pandas for Finance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.