10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sources of Data

Appendix F –how to generate a Python

dataset with an extension of .pkl or .pickle

First, let look at the simplest dataset:

>>>import pandas as pd

>>>import numpy.ranom as random

>>>x=random.randn(10)

>>>y=pd.DataFrame(x)

>>>y.to_pickle("c:/temp/test.pkl")

Reading a Python dataset with an extension of .pkl or .pickle, we use thepd.

read_pickle() function:

>>> import pandas as pd

>>>kk=pd.read_pickle("c:/temp/test.pkl")

Next, the Python program is shown to generate theffMonthly.pkl dataset:

import pandas as pd

import numpy as np

file=open("c:/temp/ffMonthly.txt","r")

data=file.readlines()

dd=mkt=smb=hml=rf=[]

n=len(data)

index=range(1,n-3)

#

for i in range(4,n):

t=data[i].split()

dd.append(pd.to_datetime(t[0]+'01', format='%Y%m%d').date())

mkt.append(float(t[1])/100)

smb.append(float(t[2])/100)

hml.append(float(t[3])/100)

rf.append(float(t[4])/100)

#

d=np.transpose([dd,mkt,smb,hml,rf])

ff=pd.DataFrame(d,index=index,columns=['DATE','MKT_

RF','SMB','HML','RF'])

ff.to_pickle("c:/temp/ffMonthly.pkl")

The first and last several observations are shown here:

>>>ff.head(2)

DATE MKT_RFSMBHML

1 1926-10-01 -0.0324 0.0004 0.0051

2 1926-11-01 0.0253 -0.002 -0.0035

[ 144 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!