Rolling Means with Missing Data

medium
Unit 14 · Time Series0/3 to milestone

Reimplement pandas' Series.rolling(window, min_periods).mean() in NumPy, including its handling of missing data:

  • The window ending at position i covers positions i - window + 1 to i.
  • NaN values inside the window are ignored.
  • If fewer than min_periods non-missing values are in the window, the result is NaN. min_periods defaults to window.

Implement rolling_mean(values, window, min_periods=None) in O(n) using cumulative sums of the values (with NaNs as 0) and of a valid-value mask. Then implement expanding_mean(values, min_periods=1), the mean of all non-missing values so far.

#time-series#numpy#missing-data2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Rolling Means with Missing Data · TechGradify