What is the purpose of resampling time series data in pandas?
Resampling changes the frequency of time series data, like converting daily data to monthly data, by aggregating or interpolating values.
Click to reveal answer
beginner
How does groupby help when resampling time data?
Using groupby lets you split data into groups (like by category) and then resample each group separately, keeping the groups distinct.
Click to reveal answer
beginner
What pandas method is used to resample time series data?
The resample() method is used to change the frequency of time series data, such as 'D' for daily or 'M' for monthly.
Click to reveal answer
beginner
Why do you need to set a datetime column as the index before resampling?
Resampling works on the datetime index, so setting the datetime column as the index tells pandas how to group data by time.
Click to reveal answer
intermediate
What does the following code do?
df.groupby('category').resample('M').sum()
It groups the data by 'category', then resamples each group to monthly frequency, summing values in each month for each category.
Click to reveal answer
Which pandas method changes the frequency of time series data?
Aresample()
Bgroupby()
Cpivot()
Dmerge()
✗ Incorrect
The resample() method changes the frequency of time series data.
Before resampling, what must you do with the datetime column?
ASet it as the DataFrame index
BConvert it to string
CDrop it
DSort it alphabetically
✗ Incorrect
The datetime column must be set as the index for resampling to work.
What does groupby('category').resample('W').mean() do?
AGroups by week only
BResamples weekly then groups by category
CGroups by category, resamples weekly, and calculates mean per group
DCalculates mean of category column
✗ Incorrect
It groups by category, resamples each group weekly, then calculates the mean.
If you want monthly sums per group, which code is correct?
Adf.resample('M').groupby('group').sum()
Bdf.groupby('group').resample('M').sum()
Cdf.groupby('group').sum().resample('M')
Ddf.sum().groupby('group').resample('M')
✗ Incorrect
You must group first, then resample each group monthly and sum.
What frequency string means 'daily' in pandas resample?
A'W'
B'M'
C'H'
D'D'
✗ Incorrect
'D' stands for daily frequency in pandas resample.
Explain how to resample time series data separately for each group in a DataFrame.
Think about the order: group first, then resample.
You got /4 concepts.
Describe why resampling requires a datetime index and what happens if you don't set it.
Consider how pandas knows how to split data by time.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using resample in pandas when working with time series data?
easy
A. To change the frequency of time-based data, like converting daily data to monthly data.
B. To sort data alphabetically by column values.
C. To merge two DataFrames based on a common key.
D. To filter rows based on a condition.
Solution
Step 1: Understand the function of resample
Resample is used to change the time frequency of data, such as converting daily data into monthly or weekly data.
Step 2: Compare with other options
Sorting, merging, and filtering are different pandas operations unrelated to resampling.
Final Answer:
To change the frequency of time-based data, like converting daily data to monthly data. -> Option A
Quick Check:
Resample changes time frequency = A [OK]
Hint: Resample changes time intervals, not sorting or filtering [OK]
Common Mistakes:
Confusing resample with sort or filter
Thinking resample merges data
Using resample without datetime index
2. Which of the following is the correct way to resample a DataFrame df by month after grouping by a column 'category' and assuming 'date' is the datetime index?
easy
A. df.resample('M').groupby('category').mean()
B. df.groupby('category').resample('M')
C. df.groupby('category').resample('M').mean()
D. df.resample('M').mean().groupby('category')
Solution
Step 1: Understand groupby and resample order
To resample within each group, first group by the column, then resample the datetime index inside each group.
Step 2: Check syntax correctness
df.groupby('category').resample('M').mean() correctly uses df.groupby('category').resample('M').mean(). Other options have wrong order or missing aggregation.
Final Answer:
df.groupby('category').resample('M').mean() -> Option C
Quick Check:
Group then resample = B [OK]
Hint: Group first, then resample on datetime index [OK]
Common Mistakes:
Resampling before grouping
Missing aggregation after resample
Not setting datetime as index before resampling
3. Given the following code, what will be the output?
A. An error because resample cannot be used after groupby.
B. A DataFrame with daily sums of 'value' for each category.
C. A DataFrame grouped by category only, ignoring dates.
D. A DataFrame with monthly sums of 'value' for each category, indexed by category and month end date.
Solution
Step 1: Understand the code flow
The DataFrame is grouped by 'category', then resampled monthly on the datetime index, summing 'value' within each month and category.
Step 2: Analyze the output structure
The result will have a MultiIndex with 'category' and the resampled month-end dates, showing sums per group per month.
Final Answer:
A DataFrame with monthly sums of 'value' for each category, indexed by category and month end date. -> Option D
Quick Check:
Group + monthly resample sum = C [OK]
Hint: Group then resample sums per month per group [OK]
Common Mistakes:
Expecting daily sums instead of monthly
Thinking resample causes error after groupby
Ignoring the datetime index requirement
4. You run this code but get an error:
df.groupby('category').resample('W').mean()
What is the most likely cause of the error?
medium
A. The 'category' column contains null values.
B. The 'date' column is not set as the DataFrame index before resampling.
C. The resample frequency 'W' is invalid.
D. You cannot use mean() after resample.
Solution
Step 1: Check resample requirements
Resample requires a datetime index. If 'date' is a column but not index, resample fails.
Step 2: Analyze error cause
Most common error is forgetting to set 'date' as index before resampling.
Final Answer:
The 'date' column is not set as the DataFrame index before resampling. -> Option B
Quick Check:
Datetime index missing causes resample error = A [OK]
Hint: Set datetime column as index before resample [OK]
Common Mistakes:
Not setting datetime index
Assuming resample frequency is wrong
Thinking aggregation like mean() is invalid
5. You have a DataFrame df with columns 'store', 'date' (datetime), and 'sales'. You want to find the total weekly sales per store, but only for weeks where the total sales exceed 1000. Which code snippet correctly does this?
hard
A. df.set_index('date').groupby('store').resample('W').sum().query('sales > 1000')
B. df.groupby('store').resample('W').sum().query('sales > 1000')
C. df.groupby('store').resample('W').sum().filter(lambda x: x['sales'] > 1000)
D. df.set_index('date').resample('W').groupby('store').sum().loc[lambda x: x['sales'] > 1000]
Solution
Step 1: Set datetime index before resampling
Resample requires the datetime column as index, so df.set_index('date') is needed.
Step 2: Group by 'store' then resample weekly and sum sales
Use groupby('store').resample('W').sum() to get weekly sales per store.
Step 3: Filter weeks where sales exceed 1000
Use query('sales > 1000') to keep only those weeks.
Final Answer:
df.set_index('date').groupby('store').resample('W').sum().query('sales > 1000') -> Option A
Quick Check:
Set index, group, resample, filter sales > 1000 = D [OK]
Hint: Set datetime index, group, resample, then filter [OK]
Common Mistakes:
Not setting datetime as index
Wrong order of groupby and resample
Using filter incorrectly instead of query
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.