1

I'm running a function called calculate_hedgeratio inside pandas.rolling_apply. The function works when called for itself, but inside rolling_apply it throws the following error:

regression = pandas.ols(x=df[xsymbol], y=df[ysymbol])

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

Here is the function calculate_hedgeratio:

def calculate_hedgeratio(df, xsymbol, ysymbol):

    import pandas
    from scipy import odr
    import numpy

    regression = pandas.ols(x=df[xsymbol], y=df[ysymbol])
    m = regression.beta[0]
    n = regression.beta[1]
    model = odr.Model(lambda B,x: (B[0]*x + B[1]))
    data = odr.RealData(df[xsymbol].values,df[ysymbol].values, sx=numpy.std(df[xsymbol].values), sy=numpy.std(df[ysymbol].values))
    myodr = odr.ODR(data, model, beta0=[m, n])
    myoutput = myodr.run()
    results = [myoutput.beta[0], myoutput.beta[1], myoutput.res_var]
    return results

This is the function from where I call it:

def simple_Spreadtest(symbol1, symbol2, startdate, enddate, lookbackperiod):
    import pandas
    import numpy

    df=pandas.DataFrame()
    df[symbol1]=numpy.random.rand(1000)
    df[symbol2]=numpy.random.rand(1000)
    df['m','n','Chired'] = pandas.rolling_apply(df,lookbackperiod, lambda x: calculate_hedgeratio(x, symbol1, symbol2))

    return df

'Main Code:'


symbol1 = 'A'   
symbol2 = 'B' 
lookbackperiod = 250
import datetime
startdate = datetime.datetime(1990, 1, 1)
enddate = datetime.datetime(2015, 7, 31)

df = simple_Spreadtest(symbol1, symbol2, startdate, enddate, lookbackperiod)

What's the reason for it?

user3276418
  • 1,777
  • 4
  • 20
  • 29

1 Answers1

0

In calculate_hedgeratio, df is actually a numpy array and not a DataFrame (which is why you are getting an index error).

This is caused by your lambda function in your rolling_apply, the latter of which can only be applied to a Series. When thisrolling_apply is applied to a DataFrame, it actually performs its calculations on each column separately.

Alexander
  • 105,104
  • 32
  • 201
  • 196
  • Thanks. It seems like I have to code the rolling window function myself. I found this approach: http://stackoverflow.com/questions/19121854/using-rolling-apply-on-a-dataframe-object, however, it uses a Python loop to iterate through the dataframe. Is there anything more efficient? – user3276418 Jul 27 '15 at 11:02