Pandas Window Functions (rolling / expanding / ewm)
Window functions perform sliding-window computations on time series or ordered data and are important tools in fields such as financial analysis and signal processing.
rolling (rolling window)
Basic Usage
Example
import
pandas
as
pd
import
numpy
as
np
Create time series data
np.
random
.
seed
(
42
)
dates
=
pd.
date_range
(
"2024-01-01"
,
periods
=
20
,
freq
=
"D"
)
ts
=
pd.
Series
(
np.
random
.
randint
(
100
,
200
,
20
)
,
index
=
dates
)
(
"Raw data (first 10 rows):"
)
(
ts.
head
(
10
)
)
(
)
7-day rolling average
ma7
=
ts.
rolling
(
window
=
7
)
.
mean
(
)
(
"7-day rolling average (first 10 rows):"
)
(
ma7.
head
(
10
)
)
(
)
Rolling sum
rolling_sum
=
ts.
rolling
(
window
=
5
)
.
sum
(
)
(
"5-day rolling sum:"
)
(
rolling_sum.
head
(
10
)
)
Types of rolling windows
Example
import
pandas
as
pd
import
numpy
as
np
s
=
pd.
Series
(
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
)
Fixed window size
(
"Fixed window (3):"
)
(
s.
rolling
(
3
)
.
mean
(
)
)
(
)
Moving window (time-based window)
1-minute trailing window
s2
=
pd.
Series
(
[
1
,
2
,
3
,
4
,
5
]
,
index
=
pd.
date_range
(
"2024-01-01"
,
periods
=
5
,
freq
=
"T"
)
)
(
"Time window (1 minute):"
)
(
s2.
rolling
(
"1min"
)
.
sum
(
)
)
expanding (expanding window)
An expanding window accumulates from the start up to the current position, with the window size growing gradually.
Example
import
pandas
as
pd
import
numpy
as
np
s
=
pd.
Series
(
[
1
,
2
,
3
,
4
,
5
]
)
Expanding window computing the cumulative mean
exp_mean
=
s.
expanding
(
)
.
mean
(
)
(
"Cumulative mean:"
)
(
exp_mean
)
(
)
Expanding window computing the cumulative maximum
exp_max
=
s.
expanding
(
)
.
max
(
)
(
"Cumulative maximum:"
)
(
exp_max
)
(
)
Expanding window computing the standard deviation
exp_std
=
s.
expanding
(
)
.
std
(
)
(
"Cumulative standard deviation:"
)
(
exp_std
)
ewm (exponentially weighted moving)
The exponentially weighted moving average (EWMA) gives more weight to recent data.
Example
import
pandas
as
pd
import
numpy
as
np
s
=
pd.
Series
(
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
)
The smaller alpha is, the greater the weight on recent data
ewm_05
=
s.
ewm
(
alpha
=
0.5
)
.
mean
(
)
ewm_2
=
s.
ewm
(
alpha
=
0.2
)
.
mean
(
)
(
"Original data:"
)
(
s.
values
)
(
"\nalpha=0.5:"
)
(
ewm_05.
values
)
(
"\nalpha=0.2:"
)
(
ewm_2.
values
)
(
)
Using span (relationship with alpha: alpha = 2/(span+1))
ewm_span
=
s.
ewm
(
span
=
5
)
.
mean
(
)
(
"span=5:"
)
(
ewm_span.
values
)
EWM is more sensitive to trend changes and suits scenarios that need a fast response.
Practical example: stock technical indicators
Example
import
pandas
as
pd
import
numpy
as
np
Simulated stock price data
np.
random
.
seed
(
42
)
dates
=
pd.
date_range
(
"2024-01-01"
,
periods
=
30
,
freq
=
"D"
)
df
=
pd.
DataFrame
(
{
"date"
: dates
,
"Closing price"
:
100
- np.
random
.
randn
(
30
)
.
cumsum
(
)
}
)
Compute technical indicators
5-day moving average
df
[
"MA5"
]
=
df
[
"Closing price"
]
.
rolling
(
5
)
.
mean
(
)
10-day moving average
df
[
"MA10"
]
=
df
[
"Closing price"
]
.
rolling
(
10
)
.
mean
(
)
5-day exponentially weighted moving average
df
[
"EMA5"
]
=
df
[
"Closing price"
]
.
ewm
(
span
=
5
)
.
mean
(
)
Volatility (5-day rolling standard deviation)
df
[
"Volatility"
]
=
df
[
"Closing price"
]
.
rolling
(
5
)
.
std
(
)
Cumulative maximum price
df
[
"Highest"
]
=
df
[
"Closing price"
]
.
expanding
(
)
.
max
(
)
(
"Stock technical indicators:"
)
(
df.
round
(
2
)
)
Comparison of window functions
| Type | Description | Applicable scenarios |
|---|---|---|
rolling |
Fixed-size window | Moving averages, volatility |
expanding |
Cumulative window | Cumulative statistics, stop-loss/take-profit |
ewm |
Exponential weighting | Trend following, fast response |