130 questions
253 votes
22 answers
474k views
How can I calculate a rolling / moving average using Python + NumPy / SciPy?
There doesn’t seem to be any function in NumPy or SciPy that simply calculate the moving average, leading to convoluted solutions. My question is twofold: What's the easiest way to (correctly) ...
119 votes
9 answers
211k views
Pandas: rolling mean by time interval
I've got a bunch of polling data; I want to compute a Pandas rolling mean to get an estimate for each day based on a three-day window. According to this question, the rolling_* functions compute the ...
24 votes
7 answers
33k views
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, ...
90 votes
5 answers
167k views
How to use rolling functions for GroupBy objects
I have a time series object grouped of the type <pandas.core.groupby.SeriesGroupBy object at 0x03F1A9F0>. grouped.sum() gives the desired result but I cannot get rolling_sum to work with the ...
41 votes
7 answers
51k views
Pandas rolling apply using multiple columns
I am trying to use a pandas.DataFrame.rolling.apply() rolling function on multiple columns. Python version is 3.7, pandas is 1.0.2. import pandas as pd #function to calculate def masscenter(x): ...
19 votes
3 answers
7k views
Adaptive moving average - top performance in R
I am looking for some performance gains in terms of rolling/sliding window functions in R. It is quite common task which can be used in any ordered observations data set. I would like to share some of ...
8 votes
2 answers
29k views
the rolling regression in R using roll apply
My imported data contains 7 variables: Y and X1, X2, X3, X4, X5, X6. I tried applying the rollapply function in zoo in order to run a rolling regression within an in-sample with a window of 262 obs. (...
0 votes
1 answer
2k views
Rolling OLS Regressions and Predictions by Group
I have a Pandas dataframe with some data on race car drivers. The relevant columns look like this: |Date |Name |Distance |avg_speed_calc |---- |---- |---- |---- |9/6/...
17 votes
5 answers
36k views
Query for count of distinct values in a rolling date range
I have a data set of email addresses and dates that those email addresses were added to a table. There can be multiple entries of an email address for various different dates. For example, if I have ...
3 votes
2 answers
417 views
Rolling computation of two simultaneous variables iteratively or rowwise, using three other given variables
The dataset named crass looks like - > dput(crass) structure(list(WT_TRADE_PRICE = c(3801, 3801, 3801, 3797, 3797, 3796.2125, 3800, 3797, 3795.09523809524, 3794, 3793, 3793, 3793.8, 3794.72, 3793....
11 votes
3 answers
8k views
Pandas rolling apply function to entire window dataframe
I want to apply a function to a rolling window. All the answers I saw here are focused on applying to a single row / column, but I would like to apply my function to the entire window. Here is a ...
3 votes
3 answers
18k views
Taking first and last value in a rolling window
Initial problem statement Using pandas, I would like to apply function available for resample() but not for rolling(). This works: df1 = df.resample(to_freq, closed='left', ...
3 votes
2 answers
714 views
Python (Polars): Vectorized operation of determining current solution with the use of previous variables
Let's say we have 3 variables a, b & c. There are n instances of each, and all but the first instance of c are null. We are to calculate each next c based on a given formula comprising of only ...
0 votes
1 answer
794 views
pandas computation on rolling 1 calendar month
I have a pandas DataFrame with date as the index and a column, 'spendings'. I intend to get the rolling max() of the 'spendings' column for the trailing 1 calendar month (not 30 days or 4 weeks). I ...
10 votes
4 answers
4k views
rolling regression by group in the tidyverse?
There are many questions about rolling regression in R, but here I am specifically looking for something that uses dplyr, broom and (if needed) purrr. This is what makes this question different. I ...