Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Window Functions (rolling / expanding / ewm)

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

)

print

(

"Raw data (first 10 rows):"

)

print

(

ts.

head

(

10

)

)

print

(

)

7-day rolling average

ma7

=

ts.

rolling

(

window

=

7

)

.

mean

(

)

print

(

"7-day rolling average (first 10 rows):"

)

print

(

ma7.

head

(

10

)

)

print

(

)

Rolling sum

rolling_sum

=

ts.

rolling

(

window

=

5

)

.

sum

(

)

print

(

"5-day rolling sum:"

)

print

(

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

print

(

"Fixed window (3):"

)

print

(

s.

rolling

(

3

)

.

mean

(

)

)

print

(

)

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"

)

)

print

(

"Time window (1 minute):"

)

print

(

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

(

)

print

(

"Cumulative mean:"

)

print

(

exp_mean

)

print

(

)

Expanding window computing the cumulative maximum

exp_max

=

s.

expanding

(

)

.

max

(

)

print

(

"Cumulative maximum:"

)

print

(

exp_max

)

print

(

)

Expanding window computing the standard deviation

exp_std

=

s.

expanding

(

)

.

std

(

)

print

(

"Cumulative standard deviation:"

)

print

(

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

(

)

print

(

"Original data:"

)

print

(

s.

values

)

print

(

"\nalpha=0.5:"

)

print

(

ewm_05.

values

)

print

(

"\nalpha=0.2:"

)

print

(

ewm_2.

values

)

print

(

)

Using span (relationship with alpha: alpha = 2/(span+1))

ewm_span

=

s.

ewm

(

span

=

5

)

.

mean

(

)

print

(

"span=5:"

)

print

(

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

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

(

)

print

(

"Stock technical indicators:"

)

print

(

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