Questions tagged [rolling-sum]
75 questions
86
votes
5 answers
Python - rolling functions for GroupBy object
I have a time series object grouped of the type . grouped.sum() gives the desired result but I cannot get rolling_sum to work with the groupby object. Is there any way to apply rolling…
user1642513
21
votes
6 answers
Sum values in a rolling/sliding window
I have the following vector:
x = c(1, 2, 3, 10, 20, 30)
At each index, 3 consecutive elements are summed, resulting in the following vector:
c(6, 15, 33, 60)
Thus, first element is 1 + 2 + 3 = 6, the second element is 2 + 3 + 10 = 15, et.c

user2834313
- 213
- 1
- 2
- 6
17
votes
6 answers
how to do forward rolling sum in pandas?
I have this dataframe:
dates = pd.date_range(start='2016-01-01', periods=20, freq='d')
df = pd.DataFrame({'A': [1] * 20 + [2] * 12 + [3] * 8,
'B': np.concatenate((dates, dates)),
'C': np.arange(40)})
I sorted…

Ahamed Moosa
- 1,395
- 7
- 16
- 30
17
votes
3 answers
Use center in pandas rolling when using a time-series
I am trying to set center=True in pandas rolling function, for a time-series:
import pandas as pd
series = pd.Series(1, index = pd.date_range('2014-01-01', '2014-04-01', freq = 'D'))
series.rolling('7D', min_periods=1, center=True,…

karen
- 822
- 1
- 6
- 22
8
votes
4 answers
Scala: Calculating the Moving Sum of a List with a fixed window
I am new to Scala and I want to calculate a moving sum with a fixed window for a list.
For example: Given the list values (1.0, 2.0, 3.0, 6.0, 7.0, 8.0, 12.0, 9.0, 4.0, 1.0), and the period 4, the function should return:
(1.0, 3.0, 6.0, 12.0,…

FlyUFalcon
- 314
- 1
- 4
- 18
8
votes
1 answer
Conditional mean and sum of previous N rows in pandas dataframe
Concerned is this exemplary pandas dataframe:
Measurement Trigger Valid
0 2.0 False True
1 4.0 False True
2 3.0 False True
3 0.0 True False
4 100.0 False …

bolla
- 359
- 1
- 3
- 10
6
votes
1 answer
Pandas groupby: Count the number of occurrences within a time range for each group
I have a dataframe:
ID DATE WIN
A 2015/6/5 Yes
A 2015/6/7 Yes
A 2015/6/7 Yes
A 2015/6/7 Yes
B 2015/6/8 No
B 2015/8/7 Yes
C 2015/5/15 Yes
C 2015/5/30 No
C 2015/7/30 No
C 2015/8/03 Yes
I want to add a column…

ChiefsCreation
- 389
- 1
- 3
- 10
5
votes
2 answers
Rolling sum in dplyr
set.seed(123)
df <- data.frame(x = sample(1:10, 20, replace = T), id = rep(1:2, each = 10))
For each id, I want to create a column which has the sum of previous 5 x values.
df %>% group_by(id) %>% mutate(roll.sum = c(x[1:4], zoo::rollapply(x, 5,…

89_Simple
- 3,393
- 3
- 39
- 94
4
votes
1 answer
Partial sum when using roll_sum
I use rolling_sum to create a 3-month rolling sum for one column. However, it creates NA for rows with now() <3 while I'd like to see partial sum there. There is a Partial argument in the documentation, but says it is not currently implemented!
Here…

Ana
- 1,516
- 3
- 15
- 26
4
votes
2 answers
Conditional sum from rows into a new column in pandas
I am looking to create a new column in panda based on the value in the row. My sample data:
df=pd.DataFrame({"A":['a','a','a','a','a','a','b','b','b'],
"Sales":[2,3,7,1,4,3,5,6,9,10,11,8,7,13,14],
"Week":[1,2,3,4,5,11,1,2,3,4])
I…

Chetanya Saxena
- 133
- 2
- 12
4
votes
1 answer
Pandas rolling sum on string column
I'm using Python3 with pandas version '0.19.2'.
I have a pandas df as follows:
chat_id line
1 'Hi.'
1 'Hi, how are you?.'
1 'I'm well, thanks.'
2 'Is it going to rain?.'
2 'No, I don't think so.'
I…

user3591836
- 953
- 2
- 16
- 29
3
votes
1 answer
Cumulative sum with a threshold window in R data.table
I want to calculate the rolling sum of n rows in my dataset where the window size 'n' depends on the sum itself. For example, I want to slide the window as soon as the rolling sum of time exceeds 5 mins. Basically, I want to calculate how much…

srishti
- 31
- 3
3
votes
4 answers
Conditional rolling sum of events with 'ragged' dates
Introduction
I am using R to analyze the 'momentum' of protest movements in Africa. To do so, I am analyzing individual protest events. I want to create a rolling measure of the rolling number (sum) of protests within a time period.
Most of the…

Yasha
- 330
- 2
- 16
3
votes
1 answer
Pandas Rolling Groupby Shift back 1, Trying to lag rolling sum
I am trying to get a rolling sum of the past 3 rows for the same ID but lagging this by 1 row. My attempt looked like the below code and i is the column. There has to be a way to do this but this method doesnt seem to work.
for i in…

William Bernard
- 359
- 4
- 15
3
votes
1 answer
pandas rolling sum if criteria is met but criteria is specified in a column
I am trying to do a rolling sum of 1000 rows. I want to sum all rows where ClosePrice is between ClosePrice_low and ClosePrice_high for each respective row and the 999 above it.
for example:
rolling count 1000: check rows 0:1000 and sum if between…

Federico Marchese
- 219
- 4
- 15