rolling.apply on custom function that requires multiple columns of dataframe to reduce single column

眉间皱痕 提交于 2019-12-11 16:47:57

问题


I am trying to create an additional column of my df['newc'] through rolling.apply on df['cond'] with a custom function. The custom function requires two columns of df. I am not sure how to get it working.

I tried

df['newc'] = df['cond'].rolling(4).apply(T_correction, 
args = (df['temp'].rolling(4)))

This is obviously not working and this gives the following error:

raise NotImplementedError('See issue #11704 {url}'.format(url=url))
NotImplementedError: See issue #11704 https://github.com/pandas-dev/pandas/issues/11704

May be rolling.apply is not appropriate here. Looking for suggestions on alternate solutions.

>>> df.head()
                       temp   cond
ts
2018-06-01 00:00:00  51.908  27.83
2018-06-01 00:05:00  52.144  27.83
2018-06-01 00:10:00  51.880  27.83
2018-06-01 00:15:00  52.001  27.83
2018-06-01 00:20:00  51.835  27.83

def T_correction(df, d):
    df = pd.DataFrame(data = df)
    df.columns = ['cond']
    df['temp'] = d
    X = df.drop(['cond'], axis = 1)    # X features: temp

    X = sm.add_constant(X)             # add intercept
    lmodel = sm.OLS(df.cond, X)        # fit the model
    results = lmodel.fit()             #
    Op = results.predict(X)            # derive LF as explained by temp
    Tc1 = df.cond - Op + np.mean(Op)   # remove the linear influence

#---conditional correction --------------------------------------
    Tc = np.where(df.temp > (np.mean(df.temp) + 0.5*np.std(df.temp)), df.cond, Tc1)
    return Tc[-1]     # returning the last value

The expected result:

>>> df.head()
                       temp   cond   newc
ts
2018-06-01 00:00:00  51.908  27.83   NaN
2018-06-01 00:05:00  52.144  27.83   NaN
2018-06-01 00:10:00  51.880  27.83   NaN
2018-06-01 00:15:00  52.001  27.83   26.00
2018-06-01 00:20:00  51.835  27.83   25.00

来源:https://stackoverflow.com/questions/57345510/rolling-apply-on-custom-function-that-requires-multiple-columns-of-dataframe-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!