how do I calculate a rolling idxmax

前端 未结 5 1741
梦如初夏
梦如初夏 2020-11-30 12:16

consider the pd.Series s

import pandas as pd
import numpy as np

np.random.seed([3,1415])
s = pd.Series(np.random.randint(0, 10, 10         


        
5条回答
  •  悲&欢浪女
    2020-11-30 12:32

    I used a generator

    def idxmax(s, w):
        i = 0
        while i + w <= len(s):
            yield(s.iloc[i:i+w].idxmax())
            i += 1
    
    pd.Series(idxmax(s, 3), s.index[2:])
    
    c    c
    d    c
    e    e
    f    e
    g    e
    h    f
    i    i
    j    i
    dtype: object
    

提交回复
热议问题