Challenge - 5 Problems
Expanding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of expanding sum with min_periods
What is the output of the following code snippet using pandas expanding sum with
min_periods=2?Pandas
import pandas as pd s = pd.Series([1, 2, 3, 4, 5]) result = s.expanding(min_periods=2).sum() print(result.tolist())
Attempts:
2 left
💡 Hint
Remember that
min_periods=2 means the expanding window will return NaN until at least 2 values are included.✗ Incorrect
The expanding sum with min_periods=2 returns NaN for the first element because there is only one value. From the second element onward, it sums all values up to that point.
❓ data_output
intermediate1:30remaining
Length of expanding window result
Given a pandas Series of length 7, what is the length of the Series returned by
expanding().mean()?Pandas
import pandas as pd s = pd.Series(range(7)) result = s.expanding().mean() print(len(result))
Attempts:
2 left
💡 Hint
The expanding window operation returns a result for each original element.
✗ Incorrect
The expanding mean returns a Series of the same length as the original Series because it computes the mean for each expanding window ending at each element.
🔧 Debug
advanced2:00remaining
Identify the error in expanding window code
What error does the following code raise when executed?
Pandas
import pandas as pd s = pd.Series([1, 2, 3]) result = s.expanding(min_periods=0).sum() print(result)
Attempts:
2 left
💡 Hint
Check the allowed values for the
min_periods parameter in expanding.✗ Incorrect
The min_periods parameter must be at least 1. Setting it to 0 raises a ValueError.
❓ visualization
advanced2:30remaining
Plotting expanding mean of a time series
Which option produces a line plot showing the original data and its expanding mean on the same graph?
Pandas
import pandas as pd import matplotlib.pyplot as plt s = pd.Series([2, 4, 6, 8, 10]) exp_mean = s.expanding().mean() plt.figure() plt.plot(s, label='Original') plt.plot(exp_mean, label='Expanding Mean') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
The expanding mean smooths the data by averaging all values up to each point.
✗ Incorrect
The expanding mean line starts at the first value and gradually approaches the original data values, showing a smoothing effect.
🚀 Application
expert3:00remaining
Using expanding window to detect trend changes
You have daily sales data in a pandas Series. You want to detect when the sales trend changes by comparing the expanding mean to the current value. Which code snippet correctly creates a boolean Series indicating if the current value is above the expanding mean?
Attempts:
2 left
💡 Hint
Expanding mean includes all data up to current point; compare current value to it.
✗ Incorrect
Option A compares the current sales value to the expanding mean correctly, producing a boolean Series indicating if sales are above the trend.