Back to Practice
#0186

Rolling Average Sales

MediumPandas25 min20 XP

Problem

Given date and sales columns, sort by date and add rolling_3_sales as the 3-row rolling mean with min_periods=1.

Why This Matters

Rolling features appear in forecasting, dashboards, anomaly checks, and behavior modeling.

Function Signature

def add_rolling_sales(df):

Examples

Example 1
Inputsales = 10, 20, 30, 40 by date
Outputrolling values 10, 15, 20, 30

The fourth row averages 20, 30, and 40.

Constraints

  • Return the exact requested result.
  • Handle common edge cases.
  • Keep the solution readable before optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputsales = 10, 20, 30, 40 by date
Expectedrolling values 10, 15, 20, 30

The fourth row averages 20, 30, and 40.

Hidden Test Categories
minimal inputrepeated valueslarger realistic input