1

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 cond = a + b*temp
    results = lmodel.fit()             #
    Op = results.predict(X)            # derive 'cond' as explained by temp
    Tc1 = df.cond - 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
Balki
  • 169
  • 2
  • 11
  • What do you want to modelise with `T_correction` ? – Alexandre B. Aug 04 '19 at 10:16
  • @Alexandre B applying some correction to `cond`, which is a function of `temp` – Balki Aug 04 '19 at 10:49
  • What is "*some corrections*" ? Are you using `ols` ? why ? – Alexandre B. Aug 04 '19 at 11:47
  • @AlexandreB. I express `cond` as a linear function of `temp` through OLS. I then remove this (`cond = a + b* temp`) from `cond`. This is `Tc1` in the function. `Tc` is where this `Tc1` is applied conditionally. – Balki Aug 04 '19 at 13:07
  • Do the solutions posted here: https://stackoverflow.com/questions/38878917/how-to-invoke-pandas-rolling-apply-with-parameters-from-multiple-column help? – hchw Aug 04 '19 at 16:46
  • @hchw yes, did come across that post. There were a few other similar posts but none addresses similar problem. – Balki Aug 04 '19 at 21:27

1 Answers1

0

It appears that this feature is currently not available. There is an issue open on this topic on pandas github. Please check: https://github.com/pandas-dev/pandas/issues/15095.

Balki
  • 169
  • 2
  • 11