Note

Pandas (35) 본문

Pandas

Pandas (35)

알 수 없는 사용자 2022. 8. 27. 00:00
728x90

How to create a dataframe with rows as strides from a given series?

L = pd.Series(range(15))

def gen_strides(a, stride_len=5, window_len=5):
    n_strides = ((a.size-window_len)//stride_len) + 1
    return np.array([a[s:(s+window_len)] for s in np.arange(0, a.size, stride_len)[:n_strides]])

gen_strides(L, stride_len=2, window_len=4)

# output
array([[ 0,  1,  2,  3],
       [ 2,  3,  4,  5],
       [ 4,  5,  6,  7],
       [ 6,  7,  8,  9],
       [ 8,  9, 10, 11],
       [10, 11, 12, 13]])

'Pandas' 카테고리의 다른 글

Pandas (37)  (0) 2022.08.29
Pandas (36)  (0) 2022.08.28
Pandas (34)  (0) 2022.08.26
Pandas (33)  (0) 2022.08.25
Pandas (32)  (0) 2022.08.24
Comments