Python pandas rolling_apply two column input into function

前端 未结 4 572
故里飘歌
故里飘歌 2020-12-08 05:25

Following on from this question Python custom function using rolling_apply for pandas, about using rolling_apply. Although I have progressed with my function, I

4条回答
  •  悲哀的现实
    2020-12-08 05:55

    All rolling_* functions works on 1d array. I'm sure one can invent some workarounds for passing 2d arrays, but in your case, you can simply precompute row-wise values for rolling evaluation:

    >>> def gm(x,p):
    ...     return ((np.cumprod(x) - 1)*p)[-1]
    ...
    >>> pd.rolling_apply(tmp['A']+tmp['B']+1, 50, lambda x: gm(x,5))
    2001-01-01   NaN
    2001-01-02   NaN
    2001-01-03   NaN
    2001-01-04   NaN
    2001-01-05   NaN
    2001-01-06   NaN
    2001-01-07   NaN
    2001-01-08   NaN
    2001-01-09   NaN
    2001-01-10   NaN
    2001-01-11   NaN
    2001-01-12   NaN
    2001-01-13   NaN
    2001-01-14   NaN
    2001-01-15   NaN
    ...
    2006-06-09   -0.000062
    2006-06-10   -0.000128
    2006-06-11    0.000185
    2006-06-12   -0.000113
    2006-06-13   -0.000962
    2006-06-14   -0.001248
    2006-06-15   -0.001962
    2006-06-16   -0.003820
    2006-06-17   -0.003412
    2006-06-18   -0.002971
    2006-06-19   -0.003882
    2006-06-20   -0.003546
    2006-06-21   -0.002226
    2006-06-22   -0.002058
    2006-06-23   -0.000553
    Freq: D, Length: 2000
    

提交回复
热议问题