Nikhil
Nikhil

Reputation: 2280

Aggregating a time series in Pandas given a window size

Lets say I have this data

a = pandas.Series([1,2,3,4,5,6,7,8])

a
Out[313]: 
0    1
1    2
2    3
3    4
4    5
5    6
6    7
7    8
dtype: int64

I would like aggregate data which groups data n rows at a time and sums them up. So if n=2 the new series would look like {3,7,11,15}.

Upvotes: 0

Views: 116

Answers (2)

Kevad
Kevad

Reputation: 2991

you can use pandas rolling mean and get it like the following: if n is your interval:

sums = list(a.rolling(n).sum()[n-1::n])

# Optional !!!
rem = len(a)%n
if rem != 0:
    sums.append(a[-rem:].sum())

The first line perfectly adds the rows if the data can be properly divided into groups, else, we also can add the remaining sum (depends on your preference). For e.g., in the above case, if n=3, then you may want to get either {6, 15, 15} or just {6, 15}. The code above is for the former case. And skipping the optional part gives you just {6, 15}.

Upvotes: 0

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

try this:

In [39]: a.groupby(a.index//2).sum()
Out[39]:
0     3
1     7
2    11
3    15
dtype: int64

In [41]: a.index//2
Out[41]: Int64Index([0, 0, 1, 1, 2, 2, 3, 3], dtype='int64')

n=3

In [42]: n=3

In [43]: a.groupby(a.index//n).sum()
Out[43]:
0     6
1    15
2    15
dtype: int64

In [44]: a.index//n
Out[44]: Int64Index([0, 0, 0, 1, 1, 1, 2, 2], dtype='int64')

Upvotes: 1

Related Questions