0
0
Pandasdata~20 mins

Rolling standard deviation in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rolling Std Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of rolling standard deviation with window=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).std()
print(result.to_list())
A[nan, nan, 0.816496580927726, 0.816496580927726, 0.816496580927726]
B[nan, nan, 0.5, 0.5, 0.5]
C[nan, nan, 1.224744871391589, 1.224744871391589, 1.224744871391589]
D[nan, nan, 1.0, 1.0, 1.0]
Attempts:
2 left
💡 Hint
Remember pandas uses sample standard deviation by default (ddof=1).
data_output
intermediate
1:30remaining
Number of non-NaN values in rolling std result
Given a pandas Series of length 10, what is the number of non-NaN values in the result of rolling standard deviation with window=4?
Pandas
import pandas as pd
s = pd.Series(range(10))
result = s.rolling(window=4).std()
count_non_nan = result.count()
print(count_non_nan)
A7
B4
C6
D10
Attempts:
2 left
💡 Hint
Rolling window of size 4 produces NaN for the first 3 elements.
🔧 Debug
advanced
1:30remaining
Identify the error in rolling std calculation
What error does the following code raise?
Pandas
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
result = s.rolling(window=3).std(ddof=0)
print(result)
AValueError: window size must be positive
BTypeError: std() got an unexpected keyword argument 'ddof'
CNo error, prints rolling std with population std (ddof=0)
DAttributeError: 'Rolling' object has no attribute 'std'
Attempts:
2 left
💡 Hint
Check if ddof is a valid argument for pandas rolling std.
🚀 Application
advanced
2:00remaining
Using rolling std to detect volatility spikes
You have a pandas Series of daily stock prices. Which code snippet correctly creates a boolean Series marking days where the 5-day rolling standard deviation exceeds 2.0?
Pandas
import pandas as pd
prices = pd.Series([10, 12, 11, 13, 15, 14, 16, 18, 17, 19])
A
volatility_spikes = prices.rolling(5).mean() > 2.0
print(volatility_spikes.tolist())
B
volatility_spikes = prices.rolling(window=5).var() > 2.0
print(volatility_spikes.tolist())
C
volatility_spikes = prices.rolling(window=5).std(ddof=0) < 2.0
print(volatility_spikes.tolist())
D
volatility_spikes = prices.rolling(window=5).std() > 2.0
print(volatility_spikes.tolist())
Attempts:
2 left
💡 Hint
Volatility is measured by standard deviation, not mean or variance threshold directly.
🧠 Conceptual
expert
2:00remaining
Effect of min_periods on rolling std output length
If you apply rolling standard deviation with window=4 and min_periods=2 on a Series of length 6, how many non-NaN values will the result contain?
Pandas
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5, 6])
result = s.rolling(window=4, min_periods=2).std()
print(result.count())
A6
B5
C3
D4
Attempts:
2 left
💡 Hint
min_periods=2 means at least 2 values are needed to compute std, even if window is 4.