rule is a string like 'D' for day, 'M' for month, 'H' for hour.
Use aggregation functions like mean(), sum() after resampling.
Examples
Resample data to monthly frequency and sum values in each month.
Pandas
df.resample('M').sum()
Resample data to hourly frequency and calculate average for each hour.
Pandas
df.resample('H').mean()
Upsample data to daily frequency and fill missing values forward.
Pandas
df.resample('D').ffill()
Sample Program
This code creates hourly data for 6 hours, then resamples it to daily sums and 2-hour averages.
Pandas
import pandas as pd
import numpy as np
# Create sample time series data with hourly frequency
rng = pd.date_range('2024-01-01', periods=6, freq='H')
data = pd.DataFrame({'value': [10, 15, 14, 20, 18, 22]}, index=rng)
# Resample to daily frequency and sum values
daily_sum = data.resample('D').sum()
# Resample to 2-hour frequency and mean values
two_hour_mean = data.resample('2H').mean()
print('Original data:')
print(data)
print('\nDaily sum:')
print(daily_sum)
print('\n2-hour mean:')
print(two_hour_mean)
OutputSuccess
Important Notes
Resampling works only if the DataFrame index is a datetime type.
Downsampling reduces data points; upsampling increases them and may need filling.
Common rules: 'D' = day, 'M' = month end, 'H' = hour, 'T' or 'min' = minute.
Summary
Resampling changes the time frequency of data for easier analysis.
Use aggregation functions like sum or mean after resampling.
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.