Python applied to economic and financial data - bootcamp examples


#1

Author: Mark Meanwell

Hi - trying the bootcamp examples via IPython notebook and had question:

This works:

# read data from Ken French's website 
ff = web.DataReader('F-F_Research_Data_Factors', 'famafrench')[0] 
# NB:  ff.xs is a conflict, rename to xsm  
ff.columns = ['xsm', 'smb', 'hml', 'rf']

But this does not (using another FamaFrench data series, '10_Industry_Portfolios):

KeyError  Traceback (most recent call last)
<ipython-input-17-115d30b96734> in <module>()
      3 
      4 # read data from Ken French's website
----> 5 ff = web.DataReader('10_Industry_Portfolios', 'famafrench')[0]
      6 
      7 ff.columns = ['NoDur','Durbl','Manuf','Enrgy','HiTec','Telcm','Shops','Hlth','Utils','Other']

KeyError: 0

Any ideas?


#2

Hi Mark,

The problem is that the DataReader function for the famafrech database returns a dictionary of DataFrame objects. It happens to be that for the F-F_Research_Data_Factors, the keys of that dict are [0, 1]. The keys for the 10_Industry_Portfolios dataset are [4, 6, 8, 10, 12, 14, 16, 18]. You can check this as follows:

In [13]: ff2.keys()
Out[13]: [4, 6, 8, 10, 12, 14, 16, 18]

In [14]: ff = web.DataReader('F-F_Research_Data_Factors', 'famafrench')

In [15]: ff.keys()
Out[15]: [0, 1]

In [16]: ff2 = web.DataReader('10_Industry_Portfolios', 'famafrench')

In [17]: ff2.keys()
Out[17]: [4, 6, 8, 10, 12, 14, 16, 18]

If you grab just one of those objects you can then operate on it how you were thinking:

In [28]: ff2_single = ff2[4]

In [29]: ff2_single.columns = ['NoDur','Durbl','Manuf','Enrgy','HiTec','Telcm','Shops','Hlth','Utils','Other']

In [30]: ff2_single.head()  # show first 5 rows
Out[30]:
        NoDur  Durbl  Manuf  Enrgy  HiTec  Telcm  Shops  Hlth  Utils  Other
192607   1.45  15.55   4.69  -1.18   2.90   0.83   0.11  1.77   7.04   2.16
192608   3.97   3.68   2.81   3.37   2.66   2.17  -0.71  4.25  -1.69   4.38
192609   1.14   4.80   1.15  -3.39  -0.38   2.41   0.21  0.69   2.04   0.29
192610  -1.24  -8.23  -3.63  -0.78  -4.58  -0.11  -2.29 -0.57  -2.63  -2.85
192611   5.21  -0.19   4.10   0.02   4.71   1.63   6.43  5.42   3.71   2.11

So, to be clear, the problem was with the [0] you had after the call to DataReader. The [0] was ok in the first example because that was one of the keys of that dict, but it was not one of the keys to the second dict.