Skip to main content
PYSPARK • LESSON 114

Moving Averages with Sliding Window Frames

How do we compute a 7-day trailing moving average of stock closing prices using relative row offsets?

Advanced2 Minutes620 XP
🤔 THE QUESTION

How do we compute a 7-day trailing moving average of stock closing prices using relative row offsets?

💡 WHAT IS IT?

Using rowsBetween(-6, Window.currentRow) to calculate a sliding aggregate across the 7 most recent trading days.

🎯 WHAT IS IT USED FOR?

Smoothing volatile time-series data, stock technical indicators, and demand trend forecasting.

💻 EXAMPLE
from pyspark.sql.window import Window
from pyspark.sql.functions import avg, col

window_spec = Window.partitionBy("stock_symbol") \
    .orderBy("trade_date") \
    .rowsBetween(-6, Window.currentRow)

df = df.withColumn("7d_moving_avg", avg(col("close_price")).over(window_spec))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Moving Averages with Sliding Window Frames.

  • Define sliding window frame with rowsBetween(-6, currentRow)
  • Compute moving average over window
  • Smooth financial time-series trends