Downloading Historical Stock Data With Pandas

This is a short post quickly outlining the Python module Pandas, which has been a great find. Pandas make the manipulation of labelled multi-dimensional data very easy indeed allowing operations that might be more familiar to users of a typical spreadsheet app to be coded up in Python in no time.

Installation of Pandas and the associated Pandas DataReader modules is a simple as this:

python -m pip install pandas
python -m pip install pandas-datareader

I’m not going to cover details of the core Pandas feature set, but rather wanted to quickly show how the DataReader modules makes the automatic collection of historical stock data very easy indeed… so for example this coder snippet shows how to download closing prices for Microsoft stock over a roughly three year time-frame, and save it to a CSV file.

import pandas_datareader.data as pdr_data
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
data = pdr_data.DataReader('MSFT', 'google', start, end)
data.to_csv("MSFT.csv")

Leave a Reply

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