Question Details

No question body available.

Tags

python pandas

Answers (2)

Accepted Answer Available
Accepted Answer
June 2, 2026 Score: 4 Rep: 34,227 Quality: High Completeness: 50%

I want to obtain the sum of the first four elements, then the sum of the next four ...

If you want a non-overlapping window, that's not a rolling window.

Instead, you can floor divide the index by your window size and group:

>>> s.groupby(s.index//4).sum()
0     6
1    22
2    38
3    54
dtype: int64

In case of a non-range index: s.groupby(np.arange(len(s)) // 4).sum()

P.S. I'm pretty sure there's a more idiomatic way to do this, but this is just what I thought of first. I'll update if I remember.

June 2, 2026 Score: 2 Rep: 15,439 Quality: Low Completeness: 40%

Not sure if there is a better way, but this is at least a work around:

s.rolling(4).sum()[3::4]

3 6.0 7 22.0 11 38.0 15 54.0 dtype: float64