A. The rolling window size must be specified with window=3
B. There is no error; the code runs correctly
C. The rolling method cannot be used on DataFrames
D. The DataFrame index must be reset before rolling
Solution
Step 1: Check rolling syntax on DataFrame
Using df.rolling(3).sum() is valid; window size can be passed as positional argument.
Step 2: Verify if code runs without error
Rolling sum on DataFrame with datetime index works fine; no reset needed.
Final Answer:
There is no error; the code runs correctly -> Option B
Quick Check:
rolling(3) on DataFrame = valid [OK]
Hint: rolling() accepts window as positional argument [OK]
Common Mistakes:
Assuming window= is mandatory
Thinking index must be reset
Believing rolling only works on Series
5. You have daily sales data in a DataFrame df with a datetime index. You want to detect if sales on a day are unusually high compared to the previous week. Which pandas method combination is best to identify these spikes?
hard
A. Sort the DataFrame by sales and pick top 7 days
B. Use diff() to find daily changes and select days with positive differences
C. Apply autocorrelation to find repeated patterns
D. Calculate 7-day rolling mean and then find days where sales exceed mean by a threshold
Solution
Step 1: Understand the goal to find spikes compared to previous week
We want to compare each day's sales to the average of the last 7 days.
Step 2: Use rolling mean to get weekly average and compare
Calculating 7-day rolling mean smooths data; days exceeding this mean by threshold indicate spikes.
Final Answer:
Calculate 7-day rolling mean and then find days where sales exceed mean by a threshold -> Option D
Quick Check:
Rolling mean + threshold = spike detection [OK]
Hint: Compare daily sales to 7-day rolling mean for spikes [OK]
Common Mistakes:
Using diff() only finds changes, not spikes
Sorting ignores time context
Autocorrelation finds patterns, not spikes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.