Compute a pandas rolling mean that starts only once the window is full

Quick Overview

Implement a pandas function that returns the simple moving average of a numeric Series and leaves NaN until a full window of observations is available. It tests the rolling window API, how pandas decides a window has enough data, index and dtype handling, and what missing values do to the result.

Compute a pandas rolling mean that starts only once the window is full

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `rolling_mean(s, window)` that returns the simple moving average of a numeric pandas Series using the pandas rolling window API, producing a value only once the window is full. Requirements: - The first `window - 1` entries must be `NaN`. - Keep the original index. The result dtype is float. For example: ```python s = pd.Series([1, 2, 3, 4, 5]) rolling_mean(s, window=3) # 0 NaN # 1 NaN # 2 2.0 # 3 3.0 # 4 4.0 # dtype: float64 ``` ```hint When a window counts as full Look at how pandas counts the observations in a window before it emits a value, and what happens when one of those observations is missing. ``` ### What a Strong Answer Covers - A rolling call that emits a value only for full windows, and the correct output on the example. - Why the first `window - 1` positions are `NaN`, and how the index and float dtype are kept. - What happens when the Series contains `NaN` values or is shorter than `window`. - The time cost of the rolling mean and whether it depends on the window size. ### Follow-up Questions - What changes if you set `min_periods=1` instead? - How would you compute an exponentially weighted mean instead? - How would you get the same result with NumPy and no `rolling`, and how would missing values affect that version? - How does the behavior change when the Series has a DatetimeIndex and the window is a time offset such as `"3D"`?

Overview: Implement a pandas function that returns the simple moving average of a numeric Series and leaves NaN until a full window of observations is available. It tests the rolling window API, how pandas decides a window has enough data, index and dtype handling, and what missing values do to the result.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement rolling_mean(s, window) that returns the simple moving average of a numeric pandas Series using the pandas rolling window API, producing a value only once the window is full.

Requirements:

  • The first window - 1 entries must be NaN .
  • Keep the original index. The result dtype is float.

For example:

s = pd.Series([1, 2, 3, 4, 5])
rolling_mean(s, window=3)
# 0    NaN
# 1    NaN
# 2    2.0
# 3    3.0
# 4    4.0
# dtype: float64

What a Strong Answer Covers Guidance

  • A rolling call that emits a value only for full windows, and the correct output on the example.
  • Why the first window - 1 positions are NaN , and how the index and float dtype are kept.
  • What happens when the Series contains NaN values or is shorter than window .
  • The time cost of the rolling mean and whether it depends on the window size.

Follow-up Questions Guidance

  • What changes if you set min_periods=1 instead?
  • How would you compute an exponentially weighted mean instead?
  • How would you get the same result with NumPy and no rolling , and how would missing values affect that version?
  • How does the behavior change when the Series has a DatetimeIndex and the window is a time offset such as "3D" ?