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 calculation
What is the output of this code snippet that calculates a rolling mean with window size 3 on a pandas Series?
Data Analysis Python
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 needs 3 values before it can compute a mean.
✗ Incorrect
The rolling mean with window 3 returns NaN for the first two values because there are not enough data points. Starting from the third value, it computes the mean of the current and previous two values.
❓ data_output
intermediate2:00remaining
Count of rolling sums above threshold
Given this pandas DataFrame, how many rolling sums with window size 4 are greater than 10?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'values': [2, 3, 5, 7, 1, 4, 6]}) rolling_sums = df['values'].rolling(window=4).sum() count = (rolling_sums > 10).sum() print(count)
Attempts:
2 left
💡 Hint
Calculate rolling sums and count how many exceed 10.
✗ Incorrect
The rolling sums with window 4 are [nan, nan, nan, 17, 16, 17, 18]. All four valid sums are greater than 10, so the count is 4.
🔧 Debug
advanced2:00remaining
Identify the error in rolling apply function
What error does this code raise when applying a custom function with rolling window?
Data Analysis Python
import pandas as pd s = pd.Series([1, 2, 3, 4, 5]) def custom_func(x): return x[0] + x[1] result = s.rolling(window=3).apply(custom_func) print(result.tolist())
Attempts:
2 left
💡 Hint
Check how the rolling window passes data to the function.
✗ Incorrect
The rolling apply function passes a numpy array whose length equals the number of available data points (less than window size at the start). The function tries to access x[1], which causes an IndexError when fewer than 2 points are available.
❓ visualization
advanced2:00remaining
Interpret rolling standard deviation plot
Given this code that plots rolling standard deviation, what pattern will the plot show?
Data Analysis Python
import pandas as pd import matplotlib.pyplot as plt import numpy as np np.random.seed(0) data = pd.Series(np.random.normal(0, 1, 100)) rolling_std = data.rolling(window=10).std() plt.plot(data, label='Data') plt.plot(rolling_std, label='Rolling Std') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Think about standard deviation of normal random data and rolling window behavior.
✗ Incorrect
The rolling standard deviation with window 10 will be NaN for the first 9 points, then it will fluctuate around the true std deviation of 1 for normal data.
🚀 Application
expert3:00remaining
Calculate rolling correlation between two series
Which option correctly calculates the rolling correlation with window size 5 between two pandas Series s1 and s2?
Data Analysis Python
import pandas as pd s1 = pd.Series([1, 2, 3, 4, 5, 6, 7]) s2 = pd.Series([7, 6, 5, 4, 3, 2, 1])
Attempts:
2 left
💡 Hint
Check pandas rolling correlation syntax in recent versions.
✗ Incorrect
The correct method is s1.rolling(window=5).corr(s2). Other options are either deprecated or invalid syntax.