Resampling means changing the frequency of your time series data. You can make data points more spread out (downsampling) or more frequent (upsampling).
Click to reveal answer
beginner
What does df.resample('M').mean() do in pandas?
It groups the data by each month ('M') and calculates the average value for each month.
Click to reveal answer
beginner
What is the difference between downsampling and upsampling?
Downsampling reduces data frequency (e.g., daily to monthly). Upsampling increases frequency (e.g., monthly to daily) and may need filling missing values.
Click to reveal answer
intermediate
Why do we need to use a method like ffill() or bfill() after upsampling?
Because upsampling creates new time points with missing values. Forward fill (ffill()) or backward fill (bfill()) fills those gaps with nearby data.
Click to reveal answer
beginner
What does the string 'W' mean in df.resample('W')?
'W' means resampling the data by week. It groups data into weekly bins.
Click to reveal answer
Which pandas function is used to change the frequency of time series data?
Aresample()
Bgroupby()
Cpivot()
Dmerge()
✗ Incorrect
The resample() function changes the frequency of time series data.
What happens when you downsample time series data?
AYou delete the data
BYou increase the number of data points
CYou keep the same number of data points
DYou reduce the number of data points
✗ Incorrect
Downsampling reduces the number of data points by grouping them into larger time bins.
After upsampling, which method can fill missing values by copying the last known value forward?
Amean()
Bffill()
Cdropna()
Dsum()
✗ Incorrect
ffill() fills missing values by carrying forward the last valid observation.
What does the frequency string 'D' stand for in pandas resampling?
ADaily
BDecade
CData
DDownsample
✗ Incorrect
'D' means daily frequency in pandas resampling.
Which aggregation function would you use to get the total sum after resampling?
Amean()
Bcount()
Csum()
Dmax()
✗ Incorrect
sum() adds all values in each resampled group.
Explain how to resample a daily time series to monthly frequency and calculate the average for each month using pandas.
Think about grouping data by month and then averaging.
You got /3 concepts.
Describe the difference between downsampling and upsampling in time series data and why filling missing values is important after upsampling.
Consider what happens to data points when changing frequency.
You got /4 concepts.
Practice
(1/5)
1. What does resampling a time series in pandas usually do?
easy
A. Sort the data by values in ascending order
B. Remove all missing values from the dataset
C. Change the time frequency of the data to summarize or expand it
D. Convert the data into a different file format
Solution
Step 1: Understand the purpose of resampling
Resampling changes how often data points appear in time, either by grouping or expanding.
Step 2: Identify what resampling affects
It changes the time frequency, not sorting or file formats.
Final Answer:
Change the time frequency of the data to summarize or expand it -> Option C
Quick Check:
Resampling = Change frequency [OK]
Hint: Resampling changes how often data points appear in time [OK]
A. The frequency string 'H' is invalid for resampling
B. Upsampling without filling missing values results in NaNs
C. Using mean() is not allowed after resampling
D. The DataFrame index must be integers, not datetime
Solution
Step 1: Identify the effect of upsampling daily to hourly
Upsampling creates new rows for each hour, but original data has only daily points.
Step 2: Recognize missing values after upsampling
New hourly rows have no data, so mean() returns NaN unless filled.
Final Answer:
Upsampling without filling missing values results in NaNs -> Option B
Quick Check:
Upsampling needs fillna() to avoid NaNs [OK]
Hint: Upsampling creates gaps; fill missing values to avoid NaNs [OK]
Common Mistakes:
Thinking 'H' frequency is invalid
Believing mean() cannot be used after resampling
Ignoring datetime index requirement
5. You have minute-level temperature data in a DataFrame df with a datetime index. You want to create a new DataFrame with hourly average temperatures but only for hours where at least 45 minutes of data exist. Which approach correctly achieves this?
hard
A. df.resample('H').mean().dropna()
B. df.resample('H').mean()
C. df.resample('H').sum().where(lambda x: x >= 45).dropna()
D. df.resample('H').apply(lambda x: x.mean() if len(x) >= 45 else None).dropna()
Solution
Step 1: Understand the requirement for minimum data points per hour
We want hourly averages only if 45 or more minutes of data exist (i.e., at least 45 rows per hour).
Step 2: Use a custom function with resample and apply
Using apply with a lambda checks the length of each group and calculates mean only if condition met; else returns None.
Step 3: Remove hours with insufficient data
Calling dropna() removes hours where the condition failed (null values).
Final Answer:
df.resample('H').apply(lambda x: x.mean() if len(x) >= 45 else None).dropna() -> Option D
Quick Check:
Custom apply filters groups by size before averaging [OK]
Hint: Use apply with condition on group size, then dropna() [OK]
Common Mistakes:
Using mean() without checking group size
Using sum() instead of mean() for temperature
Dropping NaNs without filtering groups first
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.