Financial Data
The Web today provides a wealth of financial information for free. Web giants such as
Google or Yahoo! have comprehensive financial data offerings. Although the quality of
the data sometimes does not fulfill professional requirements, for example with regard to
the handling of stock splits, such data is well suited to illustrate the “financial power” of
pandas.
To this end, we will use the pandas built-in function DataReader to retrieve stock price
data from Yahoo! Finance, analyze the data, and generate different plots of it.
[ 25 ]
The
required function is stored in a submodule of pandas:
In [ 47 ]: import pandas.io.data as web
At the time of this writing, pandas supports the following data sources:
Yahoo! Finance (yahoo)
Google Finance (google)
St. Louis FED (fred)
Kenneth French’s data library (famafrench)
World Bank (via pandas.io.wb)
We can retrieve stock price information for the German DAX index, for example, from
Yahoo! Finance with a single line of code:
In [ 48 ]: DAX = web.DataReader(name=‘^GDAXI’, data_source=‘yahoo’,
start=‘2000-1-1’)
DAX.info()
Out[48]: <class ‘pandas.core.frame.DataFrame’>
DatetimeIndex: 3760 entries, 2000-01-03 00:00:00 to 2014-09-26 00:00:00
Data columns (total 6 columns):
Open 3760 non-null float64
High 3760 non-null float64
Low 3760 non-null float64
Close 3760 non-null float64
Volume 3760 non-null int64
Adj Close 3760 non-null float64
dtypes: float64(5), int64(1)
Table 6-5 presents the parameters that the DataReader function takes.
Table 6-5. Parameters of DataReader function
Parameter Format Description
name
String
Name of data set — generally, the ticker symbol
data_source
E.g., “yahoo”
Data source
start
String/ datetime/None
Left boundary of range (default “ 2010/1/1“)
end
String/ datetime/None
Right boundary of range (default today)