Skip to content Skip to sidebar Skip to footer

Using Rolling_apply With A Function That Requires 2 Arguments In Pandas

I'm trying to use rollapply with a formula that requires 2 arguments. To my knowledge the only way (unless you create the formula from scratch) to calculate kendall tau correlation

Solution 1:

As of Pandas 0.14, rolling_apply only passes NumPy arrays to the function. A possible workaround is to pass np.arange(len(A)) as the first argument to rolling_apply, so that the tau function receives the index of the rows you wish to use. Then within the tau function,

B = A[[col1, col2]].iloc[idx]

returns a DataFrame containing all the rows required.


import numpy as np
import pandas as pd
import scipy.stats as stats
import itertools as IT

A = pd.DataFrame([[1, 5, 2], [2, 4, 4], [3, 3, 1], [4, 2, 2], [5, 1, 4]], 
                 columns=['A', 'B', 'C'], index = [1, 2, 3, 4, 5])

for col1, col2 in IT.combinations(A.columns, 2):
    def tau(idx):
        B = A[[col1, col2]].iloc[idx]
        return stats.kendalltau(B[col1], B[col2])[0]
    A[col1+col2] = pd.rolling_apply(np.arange(len(A)), 3, tau)

print(A)    

yields

   A  B  C  AB        AC        BC
1152NaNNaNNaN2244NaNNaNNaN3331-1-0.3333330.3333334422-1-0.3333330.3333335514-11.000000-1.000000

Post a Comment for "Using Rolling_apply With A Function That Requires 2 Arguments In Pandas"