I get an exception when trying to apply a custom crop function to a pandas data frame. For instance:
import statsmodels.api as sm
import pandas as pd
import numpy as np
def univar_regr_beta(y, x):
Y, X = y.as_matrix(), x.as_matrix()
X = sm.add_constant(X)
model = sm.OLS(Y, X)
return model.fit().params[1]
df = pd.DataFrame(np.random.randn(20,3))
srs = pd.Series(np.random.randn(20))
univar_regr_beta(df[0], srs)
df.rolling(5, 5).apply(np.sum)
df.rolling(5, 5).apply(lambda x: univar_regr_beta(x, srs))
In particular, I get the following:
AttributeError: 'numpy.ndarray' object has no attribute 'as_matrix'
It appears that when each column is passed to univar_regr_beta via lambda, it is passed as a bumpy array, not a series. I'm not sure if there is a better way to achieve a moving beta, or if I just missed something.
Any help is appreciated. Thanks
source
share