0
0
Data Analysis Pythondata~20 mins

Rolling window calculations in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rolling Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
A[nan, 2.0, 3.0, 4.0, 5.0]
B[nan, nan, 2.0, 3.0, 4.0]
C[1.0, 1.5, 2.0, 3.0, 4.0]
D[nan, nan, 1.0, 2.0, 3.0]
Attempts:
2 left
💡 Hint
Remember that rolling mean with window 3 needs 3 values before it can compute a mean.
data_output
intermediate
2: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)
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Calculate rolling sums and count how many exceed 10.
🔧 Debug
advanced
2: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())
ATypeError: unsupported operand type(s) for +: 'float' and 'float'
BValueError: wrong number of items passed 2, placement implies 3
CIndexError: index out of range
DNo error, outputs [nan, nan, 3.0, 5.0, 7.0]
Attempts:
2 left
💡 Hint
Check how the rolling window passes data to the function.
visualization
advanced
2: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()
ARolling std increases linearly from 0 to 10
BRolling std starts high and decreases steadily
CRolling std is constant zero throughout
DRolling std starts with NaN values then fluctuates around 1
Attempts:
2 left
💡 Hint
Think about standard deviation of normal random data and rolling window behavior.
🚀 Application
expert
3: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])
As1.rolling(window=5).corr(s2)
Bs1.corr(s2).rolling(window=5)
Cpd.rolling_corr(s1, s2, window=5)
Ds1.rolling_corr(s2, window=5)
Attempts:
2 left
💡 Hint
Check pandas rolling correlation syntax in recent versions.