0
0
Data Analysis Pythondata~20 mins

Window functions (expanding, ewm) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
A[2, 4, 6, 8]
B[2.0, 3.0, 4.0, 5.0]
C[2.0, 4.0, 6.0, 8.0]
D[2.0, 3.0, 5.0, 7.0]
Attempts:
2 left
💡 Hint
Think about how the expanding mean includes all values from the start up to the current position.
data_output
intermediate
2: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())
A[10.0, 15.0, 20.0, 25.0]
B[10.0, 20.0, 30.0, 40.0]
C[10.0, 15.0, 25.0, 35.0]
D[10.0, 15.0, 22.5, 31.25]
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.
🔧 Debug
advanced
2: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())
AThe output is [nan, nan, nan] because min_periods=4 is greater than the number of elements.
BTypeError because min_periods must be a float, not int.
CValueError because min_periods cannot be greater than the length of the series.
DThe output is [1.0, 1.5, 2.0] ignoring min_periods.
Attempts:
2 left
💡 Hint
Check what happens when min_periods is larger than the available data points.
🚀 Application
advanced
2: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?
As.ewm(halflife=3).mean()
Bs.ewm(half_life=3).mean()
Cs.ewm(alpha=3).mean()
Ds.ewm(span=3).mean()
Attempts:
2 left
💡 Hint
Check the exact parameter name for half-life in pandas ewm.
🧠 Conceptual
expert
2:00remaining
Difference between expanding and ewm window functions
Which statement correctly describes the difference between expanding and ewm window functions?
ABoth expanding and ewm apply equal weights but differ in window size.
BExpanding windows apply exponentially decreasing weights, while ewm includes all data equally from start to current point.
CExpanding windows include all data from the start to current point equally, while ewm applies exponentially decreasing weights to past data.
DExpanding windows only consider a fixed-size window, while ewm considers all data points.
Attempts:
2 left
💡 Hint
Think about how weights are assigned in each method.