Challenge - 5 Problems
Rolling Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of rolling mean with window size 3
What is the output of the following code snippet?
Pandas
import pandas as pd s = pd.Series([1, 2, 3, 4, 5]) result = s.rolling(window=3).mean() print(result.tolist())
Attempts:
2 left
💡 Hint
Remember that rolling mean with window=3 requires 3 values before it can compute the mean.
✗ Incorrect
The rolling mean with window size 3 computes the mean of the current and previous two values. The first two results are NaN because there are not enough values to fill the window.
❓ data_output
intermediate2:00remaining
Count of rolling window sums greater than 10
Given the code below, how many rolling sums with window size 4 are greater than 10?
Pandas
import pandas as pd s = pd.Series([2, 5, 3, 8, 7, 4, 6]) rolling_sum = s.rolling(window=4).sum() count = (rolling_sum > 10).sum() print(count)
Attempts:
2 left
💡 Hint
Calculate sums for windows: positions 3 to 6 and count how many exceed 10.
✗ Incorrect
The rolling sums for windows of size 4 are: NaN, NaN, NaN, (2+5+3+8)=18, (5+3+8+7)=23, (3+8+7+4)=22, (8+7+4+6)=25. All except the first three are valid. Among these, all four sums are greater than 10, so count is 4.
🔧 Debug
advanced2:00remaining
Identify the error in rolling window code
What error does the following code produce?
Pandas
import pandas as pd s = pd.Series([1, 2, 3, 4]) result = s.rolling(window=0).mean() print(result)
Attempts:
2 left
💡 Hint
Check the window parameter value and its allowed range.
✗ Incorrect
The rolling window size must be at least 1. Using window=0 raises a ValueError indicating the window must be >= 1.
🚀 Application
advanced2:30remaining
Using rolling to detect trends in stock prices
You have daily stock prices in a pandas Series. You want to create a new Series that shows the 5-day rolling average price but only for days where the average is higher than the previous day's average. Which code snippet correctly achieves this?
Attempts:
2 left
💡 Hint
Compare the rolling average to its previous value using shift(1).
✗ Incorrect
Option B correctly computes the 5-day rolling mean and filters to keep only days where the rolling average is greater than the previous day's rolling average.
🧠 Conceptual
expert2:30remaining
Effect of min_periods in rolling windows
Consider a pandas Series with 6 values. You apply rolling(window=4, min_periods=2).sum(). How many values in the resulting Series will be non-NaN?
Attempts:
2 left
💡 Hint
min_periods=2 means the sum is computed if at least 2 values are present in the window.
✗ Incorrect
With window=4 and min_periods=2, the rolling sum starts producing values from the second element onward, so positions 1 to 5 (5 values) will have sums, the first position is NaN.