Pandas Window Functions (rolling / expanding / ewm)

Window functions are used to perform sliding window calculations 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("Original 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))

Rolling Window Types

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 window)
# Window after 1 minute
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

The expanding window accumulates from the beginning to the current position, with the window size gradually increasing.

Example

import pandas as pd
import numpy as np

s = pd.Series([1, 2, 3, 4, 5])

# Expanding window to calculate cumulative mean
exp_mean = s.expanding().mean()
print("Cumulative mean:")
print(exp_mean)
print()

# Expanding window to calculate cumulative maximum
exp_max = s.expanding().max()
print("Cumulative maximum:")
print(exp_max)
print()

# Expanding window to calculate standard deviation
exp_std = s.expanding().std()
print("Cumulative standard deviation:")
print(exp_std)

ewm Exponentially Weighted Moving

The Exponentially Weighted Moving Average (EWMA) assigns greater 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 the alpha, the greater the weight of 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 is suitable for scenarios that require rapid response.


Hands-on: Stock Technical Indicators

Example

import pandas as pd
import numpy as np

# Simulate stock price data
np.random.seed(42)
dates = pd.date_range("2024-01-01", periods=30, freq="D")
df = pd.DataFrame({
    "Date": dates,
    "Close": 100 + np.random.randn(30).cumsum()
})

# Calculate technical indicators
# 5-day moving average
df["MA5"] = df["Close"].rolling(5).mean()

# 10-day moving average
df["MA10"] = df["Close"].rolling(10).mean()

# 5-day exponentially weighted moving average
df["EMA5"] = df["Close"].ewm(span=5).mean()

# Volatility (5-day rolling standard deviation)
df["Volatility"] = df["Close"].rolling(5).std()

# Cumulative highest price
df["Highest"] = df["Close"].expanding().max()

print("Stock technical indicators:")
print(df.round(2))

Window Function Comparison

Type Description Use Cases
rolling Fixed-size Window Moving average, volatility
expanding Cumulative Window Cumulative statistics, stop-loss/take-profit
ewm Exponentially Weighted Trend tracking, rapid response
Other Extensions