0
0
Pandasdata~20 mins

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

Choose your learning style9 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.