B. Wrong frequency string 'W' for weekly resampling
C. DataFrame index is not a datetime index
D. Aggregation function 'mean' does not exist
Solution
Step 1: Check the resample syntax
The code uses mean without parentheses, so it returns a method, not the result.
Step 2: Verify frequency and index
'W' is correct for weekly, and the index is a datetime index, so no issues there.
Final Answer:
Missing parentheses after 'mean' function call -> Option A
Quick Check:
Call mean() with () to get result [OK]
Hint: Add () after mean to execute function [OK]
Common Mistakes:
Forgetting parentheses on aggregation functions
Thinking 'W' is invalid frequency
Not setting datetime index before resampling
5. You have minute-level temperature data in a DataFrame df indexed by datetime. You want to resample it to hourly data but only keep hours where the average temperature is above 20°C. Which code snippet correctly does this?
hard
A. df.resample('H').mean()[lambda x: x['temperature'] > 20]
B. df.resample('H').mean().query('temperature > 20')
C. df.resample('H').mean()[df.resample('H').mean() > 20]
D. df.resample('H').mean().filter(lambda x: x > 20)
Solution
Step 1: Resample to hourly and calculate mean
Use df.resample('H').mean() to get hourly averages.
Step 2: Filter rows where temperature > 20
Using .query('temperature > 20') filters rows correctly by the 'temperature' column.
Final Answer:
df.resample('H').mean().query('temperature > 20') -> Option B
Quick Check:
Use query() to filter by column condition [OK]
Hint: Use query() after resample mean to filter rows [OK]
Common Mistakes:
Applying boolean filter on DataFrame without column name
Using filter() incorrectly for row filtering
Repeating resample twice causing inefficiency
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.