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