Compute a Time-Based 7-Day Rolling Mean in Pandas

Quick Overview

Compute a 7-day rolling mean in pandas over a datetime-indexed Series that has missing days. It tests time-based windows versus row-count windows, which timestamps fall on each window boundary, the min_periods default, and the index rules for offset rolling.

Compute a Time-Based 7-Day Rolling Mean in Pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `rolling_7day_mean(s: pd.Series) -> pd.Series`. The input is a numeric `Series` with a roughly daily `DatetimeIndex`, possibly missing days. Return the 7-day rolling mean: each point averages all observations in the 7 days ending at that point's timestamp. The point itself is included, and an observation exactly 7 days earlier falls outside the window. Requirements: - Window is the time offset `'7D'`, so index gaps do not distort it the way a fixed 7-row window would. - Result keeps the input's `DatetimeIndex`. - Early windows use available observations, so the first points are not NaN. Example: ```python import pandas as pd idx = pd.to_datetime(['2024-01-01','2024-01-03','2024-01-08']) s = pd.Series([10, 20, 40], index=idx) rolling_7day_mean(s) # 2024-01-01 10.0 # {01-01} # 2024-01-03 15.0 # {01-01, 01-03} # 2024-01-08 30.0 # {01-03, 01-08} (01-01 falls outside 7D) ``` ```hint Rows versus time Three rows in the example span eight days. Look for a way to give `rolling` a time span where you would normally give it a row count. ``` ```hint Check the boundary On 2024-01-08 the observation from 2024-01-01 is dropped. Work out which end of the window is open and whether that matches the library default. ``` ### What a Strong Answer Covers - Uses a time-offset window and explains why a 7-row window gives wrong averages when days are missing. - States exactly which timestamps fall in each window, including the open end 7 days back. - Explains the `min_periods` default for offset windows and what the first points return. - Covers the index preconditions: a `DatetimeIndex` in time order, and what duplicate timestamps do. - Gives the time and space cost. ### Follow-up Questions - How does `'7D'` differ from `rolling(7)`? - What if the index is unsorted or has duplicate timestamps? - How would you compute the mean of the 7 days before each point, excluding the point itself? - The data holds daily values for many users in long format. How do you compute a 7-day rolling mean per user?

Overview: Compute a 7-day rolling mean in pandas over a datetime-indexed Series that has missing days. It tests time-based windows versus row-count windows, which timestamps fall on each window boundary, the min_periods default, and the index rules for offset rolling.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement rolling_7day_mean(s: pd.Series) -> pd.Series. The input is a numeric Series with a roughly daily DatetimeIndex, possibly missing days. Return the 7-day rolling mean: each point averages all observations in the 7 days ending at that point's timestamp. The point itself is included, and an observation exactly 7 days earlier falls outside the window.

Requirements:

  • Window is the time offset '7D' , so index gaps do not distort it the way a fixed 7-row window would.
  • Result keeps the input's DatetimeIndex .
  • Early windows use available observations, so the first points are not NaN.

Example:

import pandas as pd
idx = pd.to_datetime(['2024-01-01','2024-01-03','2024-01-08'])
s = pd.Series([10, 20, 40], index=idx)
rolling_7day_mean(s)
# 2024-01-01    10.0   # {01-01}
# 2024-01-03    15.0   # {01-01, 01-03}
# 2024-01-08    30.0   # {01-03, 01-08}  (01-01 falls outside 7D)

What a Strong Answer Covers Guidance

  • Uses a time-offset window and explains why a 7-row window gives wrong averages when days are missing.
  • States exactly which timestamps fall in each window, including the open end 7 days back.
  • Explains the min_periods default for offset windows and what the first points return.
  • Covers the index preconditions: a DatetimeIndex in time order, and what duplicate timestamps do.
  • Gives the time and space cost.

Follow-up Questions Guidance

  • How does '7D' differ from rolling(7) ?
  • What if the index is unsorted or has duplicate timestamps?
  • How would you compute the mean of the 7 days before each point, excluding the point itself?
  • The data holds daily values for many users in long format. How do you compute a 7-day rolling mean per user?