pandas rolling computation with window based on values instead of counts

后端 未结 3 2046
暗喜
暗喜 2020-12-04 17:17

I\'m looking for a way to do something like the various rolling_* functions of pandas, but I want the window of the rolling computation to be defin

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 17:48

    Based on Zelazny7's answer, I created this more general solution:

    def rollBy(what, basis, window, func):
        def applyToWindow(val):
            chunk = what[(val<=basis) & (basis>> rollBy(d.ToRoll, d.RollBasis, 5, sum)
    0    -4
    1    -4
    2    -4
    3    -4
    4    -6
    5    -2
    6   -15
    7   -20
    8    -7
    9    -5
    Name: RollBasis
    

    It's still not ideal as it is very slow compared to rolling_apply, but perhaps this is inevitable.

提交回复
热议问题