Compute a pandas rolling sum that allows partial windows at the start
Quick Overview
Implement a pandas function that returns a rolling sum over a fixed number of observations while still giving the first positions a value from a shorter window. It tests the rolling window API, index and dtype handling, and how missing values inside a window change the result.
Compute a pandas rolling sum that allows partial windows at the start
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `rolling_sum_partial(s: pd.Series, window: int) -> pd.Series` that returns the rolling sum over `window` observations using the pandas rolling window API, allowing partial windows so every position has a value.
Requirements:
- Every position, including the first, is numeric (no `NaN`) when `s` has no missing values.
- Preserve the original index of `s`.
- The result dtype is float.
For example:
```python
s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
rolling_sum_partial(s, window=3)
# a 1.0 # [1]
# b 3.0 # [1,2]
# c 6.0 # [1,2,3]
# d 9.0 # [2,3,4]
# dtype: float64
```
```hint Short windows at the start
Find the `rolling` argument that decides how many observations a window needs before pandas returns a value.
```
### What a Strong Answer Covers
- A rolling call that returns a sum for the short windows at the start, with the correct output on the example.
- How the index and the float dtype come out of the rolling sum.
- How missing values inside a window change the sum, including a window with no valid values at all.
- The time and space cost.
### Follow-up Questions
- How does behavior differ without `min_periods=1`?
- How would you handle `NaN` values within the Series?
- Why can the partial-window sums at the start be misleading next to full-window sums, and how would you report them?
Overview: Implement a pandas function that returns a rolling sum over a fixed number of observations while still giving the first positions a value from a shorter window. It tests the rolling window API, index and dtype handling, and how missing values inside a window change the result.
Implement rolling_sum_partial(s: pd.Series, window: int) -> pd.Series that returns the rolling sum over window observations using the pandas rolling window API, allowing partial windows so every position has a value.
Requirements:
Every position, including the first, is numeric (no
NaN
) when
s
has no missing values.
Preserve the original index of
s
.
The result dtype is float.
For example:
s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])
rolling_sum_partial(s, window=3)
# a 1.0 # [1]
# b 3.0 # [1,2]
# c 6.0 # [1,2,3]
# d 9.0 # [2,3,4]
# dtype: float64
What a Strong Answer Covers Guidance
A rolling call that returns a sum for the short windows at the start, with the correct output on the example.
How the index and the float dtype come out of the rolling sum.
How missing values inside a window change the sum, including a window with no valid values at all.
The time and space cost.
Follow-up Questions Guidance
How does behavior differ without
min_periods=1
?
How would you handle
NaN
values within the Series?
Why can the partial-window sums at the start be misleading next to full-window sums, and how would you report them?