O'Reilly logo

Mastering pandas for Finance by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required