Bird
Raised Fist0
Pandasdata~20 mins

Resampling time series data in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Resampling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this resampling code?
Given a time series with daily data, what is the output after resampling to monthly frequency using sum aggregation?
Pandas
import pandas as pd
import numpy as np
rng = pd.date_range('2023-01-01', periods=5, freq='D')
data = pd.Series([1, 2, 3, 4, 5], index=rng)
result = data.resample('M').sum()
print(result)
A
2023-01-31    15
Freq: M, dtype: int64
B
2023-01-05    15
Freq: D, dtype: int64
C
2023-01-01    1
2023-01-02    2
2023-01-03    3
2023-01-04    4
2023-01-05    5
Freq: D, dtype: int64
D
2023-01-31    5
Freq: M, dtype: int64
Attempts:
2 left
💡 Hint
Think about what resampling to 'M' (month end) does and how sum aggregates daily values.
❓ data_output
intermediate
1:30remaining
How many rows are in the resampled DataFrame?
A DataFrame has hourly data for 2 days. After resampling to daily frequency using mean aggregation, how many rows does the result have?
Pandas
import pandas as pd
import numpy as np
rng = pd.date_range('2023-01-01', periods=48, freq='H')
data = pd.DataFrame({'value': np.arange(48)}, index=rng)
result = data.resample('D').mean()
print(len(result))
A2
B1
C24
D48
Attempts:
2 left
💡 Hint
Resampling groups data by day. How many days are in 48 hours?
🔧 Debug
advanced
2:00remaining
What error does this resampling code raise?
Identify the error raised by this code snippet:
Pandas
import pandas as pd
import numpy as np
rng = pd.date_range('2023-01-01', periods=5, freq='D')
data = pd.Series(np.arange(5), index=rng)
result = data.resample('M').mean()
result = result.resample('H').sum()
AAttributeError: 'Series' object has no attribute 'resample'
BValueError: Cannot resample from monthly to hourly frequency
CNo error, code runs successfully
DTypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Float64Index'
Attempts:
2 left
💡 Hint
Check the index type after the first resample and if resample supports chaining.
🚀 Application
advanced
2:30remaining
Which option produces the correct weekly resampled sum?
Given daily sales data, which code correctly resamples to weekly sums with weeks starting on Monday?
Pandas
import pandas as pd
import numpy as np
rng = pd.date_range('2023-01-01', periods=14, freq='D')
data = pd.Series(np.arange(14), index=rng)
Adata.resample('7D').sum()
Bdata.resample('W-MON').sum()
Cdata.resample('W-SUN').sum()
Ddata.resample('W').sum()
Attempts:
2 left
💡 Hint
Check the alias for weekly resampling starting on Monday.
🧠 Conceptual
expert
3:00remaining
What is the effect of using 'label' and 'closed' parameters in resample?
In pandas resample, what do the 'label' and 'closed' parameters control?
A'label' controls the index type after resampling; 'closed' controls whether missing data is filled
B'label' controls the aggregation function; 'closed' controls the frequency of resampling
C'label' controls the sorting order of the result; 'closed' controls the data type of the output
D'label' controls the timestamp assigned to the resampled bin; 'closed' controls which side of the interval is included
Attempts:
2 left
💡 Hint
Think about how intervals are labeled and which endpoints are included in resampling bins.

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

  1. Step 1: Understand the purpose of resampling

    Resampling changes how often data points appear in time, either by grouping or expanding.
  2. Step 2: Identify what resampling affects

    It changes the time frequency, not sorting or file formats.
  3. Final Answer:

    Change the time frequency of the data to summarize or expand it -> Option C
  4. Quick Check:

    Resampling = Change frequency [OK]
Hint: Resampling changes how often data points appear in time [OK]
Common Mistakes:
  • Confusing resampling with sorting data
  • Thinking resampling removes missing values automatically
  • Believing resampling changes file formats
2. Which of the following is the correct syntax to downsample a pandas DataFrame df with a datetime index to monthly frequency and calculate the mean?
easy
A. df.resample('M').mean()
B. df.resample('M').sum()
C. df.resample('D').mean()
D. df.resample('M').fillna()

Solution

  1. Step 1: Identify the correct resample frequency for monthly data

    'M' stands for month-end frequency in pandas resampling.
  2. Step 2: Choose the correct aggregation function

    To calculate the mean after downsampling, use .mean().
  3. Final Answer:

    df.resample('M').mean() -> Option A
  4. Quick Check:

    Monthly mean = df.resample('M').mean() [OK]
Hint: Use 'M' for monthly and .mean() to aggregate [OK]
Common Mistakes:
  • Using 'D' instead of 'M' for monthly frequency
  • Using sum() when mean() is asked
  • Calling fillna() without upsampling
3. Given the following code, what will be the output of print(resampled)?
import pandas as pd
idx = pd.date_range('2023-01-01', periods=4, freq='D')
data = [10, 20, 30, 40]
df = pd.DataFrame({'value': data}, index=idx)
resampled = df.resample('2D').sum()
medium
A. value 2023-01-01 30 2023-01-03 70
B. value 2023-01-01 10 2023-01-02 20 2023-01-03 30 2023-01-04 40
C. value 2023-01-01 15 2023-01-03 35
D. {'2023-01-01': 30, '2023-01-03': 70}

Solution

  1. Step 1: Understand resampling with '2D' and sum aggregation

    Resampling with '2D' groups data every 2 days and sums values in each group.
  2. Step 2: Calculate sums for each 2-day period

    First period: 2023-01-01 + 2023-01-02 = 10 + 20 = 30; Second period: 2023-01-03 + 2023-01-04 = 30 + 40 = 70.
  3. Final Answer:

    value 2023-01-01 30 2023-01-03 70 -> Option A
  4. Quick Check:

    Sum every 2 days = 30, 70 [OK]
Hint: Sum values in 2-day groups after resampling [OK]
Common Mistakes:
  • Confusing sum with mean aggregation
  • Expecting original daily data instead of grouped
  • Misreading the index labels after resampling
4. What is wrong with this code snippet for upsampling daily data to hourly data?
df = pd.DataFrame({'value': [1, 2]}, index=pd.date_range('2023-01-01', periods=2, freq='D'))
upsampled = df.resample('H').mean()
medium
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

  1. Step 1: Identify the effect of upsampling daily to hourly

    Upsampling creates new rows for each hour, but original data has only daily points.
  2. Step 2: Recognize missing values after upsampling

    New hourly rows have no data, so mean() returns NaN unless filled.
  3. Final Answer:

    Upsampling without filling missing values results in NaNs -> Option B
  4. 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

  1. 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).
  2. 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.
  3. Step 3: Remove hours with insufficient data

    Calling dropna() removes hours where the condition failed (null values).
  4. Final Answer:

    df.resample('H').apply(lambda x: x.mean() if len(x) >= 45 else None).dropna() -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard