We already showed how to download the factor-related data directly from prof. French's website. As an alternative, we can use the functionalities of pandas_datareader and avoid some manual preprocessing steps:
- Import the libraries:
from pandas_datareader.famafrench import get_available_datasets
import pandas_datareader.data as web
- Print the available datasets (only the first five):
get_available_datasets()[:5]
The preceding code results in the following output:
['F-F_Research_Data_Factors',
'F-F_Research_Data_Factors_weekly',
'F-F_Research_Data_Factors_daily',
'F-F_Research_Data_5_Factors_2x3',
'F-F_Research_Data_5_Factors_2x3_daily']
- Download the selected dataset:
ff_dict = web.DataReader('F-F_Research_Data_Factors', 'famafrench',
start='2014-01-01')
The default behavior of web.DataReader downloads the last 5 years' worth of data. The resulting object is a dictionary and we can inspect its contents by running the following command:
ff_dict.keys()
This results in the following output:
dict_keys([0, 1, 'DESCR'])
- Inspect the description of the dataset:
print(ff_dict['DESCR'])
The description is as follows:
- View the monthly dataset:
ff_dict[0].head()
The dataset looks like the one we manually downloaded from prof. French's website:
The yearly values are stored under the key of 1 and can be accessed by using ff_dict[1].