In [1]:
%matplotlib inline
import numpy as np
import pandas as pd
import os
In [4]:
path_data = 'G:/APM/Dados/MONET FINANC/Indicadores inflação/InfSwaps.xlsx'
In [5]:
df_raw = pd.read_excel(path_data,sheetname='data',skiprows=[0,1,3,4],index_col=0)
df_raw.head()
Out[5]:
In [12]:
df_raw.to_csv('raw_data.csv',header=True,index=True,index_label='date')
In [6]:
df_raw0 = df_raw
In [7]:
df_raw = df_raw0
In [8]:
cmask = [cname for cname in df_raw.columns if 'Unnamed' not in cname]
df_raw = df_raw.loc[:,cmask]
df_raw.head()
Out[8]:
tt = [cname.split()[0].split('EUSWI')[1] for cname in df_raw.columns if 'CMPN' in cname ] len(tt)
df_raw.columns = [c.split()[0].split('EUSWI')[1] + 'y' for c in df_raw.columns]
In [11]:
df_raw.tail()
Out[11]:
In [97]:
def getForward(v,t1=1,t2=2):
return (np.power(np.power(1+v[1]/100,t2)/np.power(1+v[0]/100,t1),1/(t2-t1))-1)*100
In [98]:
ind1 = 0
ind2 = 1
v2 = df_raw.iloc[-1,ind2]
v1 = df_raw.iloc[-1,ind1]
t1 = int(df_raw.columns[ind1].strip('y'))
t2 = int(df_raw.columns[ind2].strip('y'))
print('v1 is {}, v2 is {}'.format(v1,v2))
In [99]:
v = [v1,v2]
f = getForward(v,t1,t2)
In [100]:
f
Out[100]:
In [101]:
df_raw['1yf1y'] = pd.Series('',index = df_raw.index)
df_raw.tail()
Out[101]:
In [102]:
df_raw['1yf1y'] = df_raw[['1y','2y']].apply(getForward,axis=1)
In [103]:
df_raw['1yf1y'] = df_raw[['1y','2y']].apply(lambda x: getForward(x,t1=1,t2=2),axis=1)
In [104]:
df_raw.tail()
Out[104]:
In [105]:
def AddForward(df,t1=1,t2=2):
#add an empty column
df[str(t2-t1)+'yf'+str(t1)+'y'] = pd.Series('',index = df.index)
df[str(t2-t1)+'yf'+str(t1)+'y'] = df[[str(t1)+'y',str(t2)+'y']].apply(lambda x: getForward(x,t1,t2),axis=1)
In [110]:
AddForward(df_raw,t1=3,t2=4)
In [111]:
df_raw.tail()
Out[111]:
In [82]:
t1,t2=1,3
In [86]:
#df[str(t2-t1)+'yf'+str(t1)+'y'] = pd.Series('',index = df.index)
df.tail()
Out[86]:
df_raw['2yf1y'] = df_raw[['1y','3y']].apply(lambda x: getForward(x,t1=1,t2=3),axis=1)
In [88]:
df_raw[['1y','3y']]
Out[88]:
In [85]:
df[str(t2-t1)+'yf'+str(t1)+'y'] = df[[str(t1)+'y',str(t2)+'y']].apply(lambda x: getForward(x,t1,t2),axis=1)
In [ ]: