Challenge - 5 Problems
Window Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of expanding mean calculation
What is the output of the following code snippet that uses the expanding window function to calculate the mean?
Data Analysis Python
import pandas as pd s = pd.Series([2, 4, 6, 8]) result = s.expanding().mean() print(result.tolist())
Attempts:
2 left
💡 Hint
Think about how the expanding mean includes all values from the start up to the current position.
✗ Incorrect
The expanding mean at each position is the average of all values from the first element up to that position. So for the series [2,4,6,8], the means are: 2, (2+4)/2=3, (2+4+6)/3=4, (2+4+6+8)/4=5.
❓ data_output
intermediate2:00remaining
Result of exponential weighted mean with alpha=0.5
Given the following code using exponential weighted mean (ewm), what is the resulting list of values?
Data Analysis Python
import pandas as pd s = pd.Series([10, 20, 30, 40]) result = s.ewm(alpha=0.5, adjust=False).mean() print(result.tolist())
Attempts:
2 left
💡 Hint
Remember that with adjust=False, the ewm mean is calculated recursively using the formula: new_mean = alpha * current_value + (1 - alpha) * previous_mean.
✗ Incorrect
Starting with 10, the next values are calculated as: 15 = 0.5*20 + 0.5*10, 22.5 = 0.5*30 + 0.5*15, 31.25 = 0.5*40 + 0.5*22.5.
🔧 Debug
advanced2:00remaining
Identify the error in expanding window code
What error will this code raise and why?
Data Analysis Python
import pandas as pd s = pd.Series([1, 2, 3]) result = s.expanding(min_periods=4).mean() print(result.tolist())
Attempts:
2 left
💡 Hint
Check what happens when min_periods is larger than the available data points.
✗ Incorrect
The expanding window requires at least min_periods data points to compute the mean. Since the series has only 3 elements and min_periods=4, all results are NaN.
🚀 Application
advanced2:00remaining
Choosing parameters for ewm to smooth data
You have daily sales data with sudden spikes. You want to smooth the data using ewm with a half-life of 3 days. Which code snippet correctly applies this smoothing?
Attempts:
2 left
💡 Hint
Check the exact parameter name for half-life in pandas ewm.
✗ Incorrect
The correct parameter name is 'halflife' (all lowercase, no underscore). Using 'halflife=3' applies exponential smoothing with a half-life of 3 periods.
🧠 Conceptual
expert2:00remaining
Difference between expanding and ewm window functions
Which statement correctly describes the difference between expanding and ewm window functions?
Attempts:
2 left
💡 Hint
Think about how weights are assigned in each method.
✗ Incorrect
Expanding windows calculate statistics using all data points from the start up to the current point with equal weight. Ewm applies weights that decrease exponentially for older data points, giving more importance to recent data.