Pandas Window Functions (rolling / expanding / ewm)
\n\nWindow functions are used for sliding-window calculations on time series or ordered data, and are essential tools in fields such as financial analysis and signal processing.
\n\n\n\n
rolling: Sliding Window
\n\nBasic Usage
\n\nExample
\nimport pandas as pd\n\nimport numpy as np\n\n# Create time series data\n\nnp.random.seed(42)\n\ndates = pd.date_range("2024-01-01", periods=20, freq="D")\n\nts = pd.Series(np.random.randint(100,200,20), index=dates)\n\nprint("Original data (first 10 rows):")\n\nprint(ts.head(10))\n\nprint()\n\n# 7-day rolling average\n\nma7 = ts.rolling(window=7).mean()\n\nprint("7-day rolling average (first 10 rows):")\n\nprint(ma7.head(10))\n\nprint()\n\n# Rolling sum\n\nrolling_sum = ts.rolling(window=5).sum()\n\nprint("5-day rolling sum:")\n\nprint(rolling_sum.head(10))\n\n\nSliding Window Types
\n\nExample
\nimport pandas as pd\n\nimport numpy as np\n\ns = pd.Series([1,2,3,4,5,6,7,8,9,10])\n\n# Fixed window size\n\nprint("Fixed window (3):")\n\nprint(s.rolling(3).mean())\n\nprint()\n\n# Moving window (time-based window)\n\n# 1-minute window\n\ns2 = pd.Series([1,2,3,4,5], index=pd.date_range("2024-01-01", periods=5, freq="T"))\n\nprint("Time-based window (1 minute):")\n\nprint(s2.rolling("1min").sum())\n\n\n\n\n
expanding: Expanding Window
\n\nThe expanding window accumulates from the start up to the current position, gradually increasing in size.
\n\nExample
\nimport pandas as pd\n\nimport numpy as np\n\ns = pd.Series([1,2,3,4,5])\n\n# Expanding window: cumulative mean\n\nexp_mean = s.expanding().mean()\n\nprint("Cumulative mean:")\n\nprint(exp_mean)\n\nprint()\n\n# Expanding window: cumulative maximum\n\nexp_max = s.expanding().max()\n\nprint("Cumulative maximum:")\n\nprint(exp_max)\n\nprint()\n\n# Expanding window: cumulative standard deviation\n\nexp_std = s.expanding().std()\n\nprint("Cumulative standard deviation:")\n\nprint(exp_std)\n\n\n\n\n
ewm: Exponentially Weighted Moving Average
\n\nExponentially Weighted Moving Average (EWMA) assigns greater weight to more recent data points.
\n\nExample
\nimport pandas as pd\n\nimport numpy as np\n\ns = pd.Series([1,2,3,4,5,6,7,8,9,10])\n\n# Smaller alpha gives higher weight to recent data\n\newm_05 = s.ewm(alpha=0.5).mean()\n\newm_2 = s.ewm(alpha=0.2).mean()\n\nprint("Original data:")\n\nprint(s.values)\n\nprint("nalpha=0.5:")\n\nprint(ewm_05.values)\n\nprint("nalpha=0.2:")\n\nprint(ewm_2.values)\n\nprint()\n\n# Using span (relation to alpha: alpha = 2/(span+1))\n\newm_span = s.ewm(span=5).mean()\n\nprint("span=5:")\n\nprint(ewm_span.values)\n\n\n\nEWM is more sensitive to trend changes and suitable for scenarios requiring rapid response.
\n\n
Practical Application: Stock Technical Indicators
\n\nExample
\nimport pandas as pd\n\nimport numpy as np\n\n# Simulate stock price data\n\nnp.random.seed(42)\n\ndates = pd.date_range("2024-01-01", periods=30, freq="D")\n\ndf = pd.DataFrame({\n\n"Date": dates,\n\n"Closing Price": 100 + np.random.randn(30).cumsum()\n\n})\n\n# Compute technical indicators\n\n# 5-day moving average\n\ndf= df.rolling(5).mean()\n\n# 10-day moving average\n\ndf= df.rolling(10).mean()\n\n# 5-day exponential moving average\n\ndf= df.ewm(span=5).mean()\n\n# Volatility (5-day rolling standard deviation)\n\ndf= df.rolling(5).std()\n\n# Cumulative highest price\n\ndf= df.expanding().max()\n\nprint("Stock technical indicators:")\n\nprint(df.round(2))\n\n\n\n\n
Comparison of Window Functions
\n\n| Type | \nDescription | \nApplicable Scenarios | \n
|---|---|---|
rolling | \n Fixed-size window | \nMoving averages, volatility | \n
expanding | \n Cumulative window | \nCumulative statistics, stop-loss/take-profit | \n
ewm | \n Exponentially weighted | \nTrend tracking, rapid response | \n
YouTip