📖 Scenario: You work as a data analyst for a weather station. You have collected temperature data every hour, but your manager wants to see the average temperature for each day instead. To do this, you need to resample the hourly data into daily data.
🎯 Goal: Learn how to resample time series data from hourly to daily frequency and calculate the daily average temperature.
📋 What You'll Learn
Create a pandas DataFrame with hourly temperature data and datetime index
Create a variable for the resampling frequency
Use pandas resample method to calculate daily average temperature
Print the resulting daily average temperature DataFrame
💡 Why This Matters
🌍 Real World
Weather stations and many other fields collect data at high frequency but often need summaries at lower frequency for reports or analysis.
💼 Career
Data analysts and scientists frequently resample time series data to extract meaningful insights and prepare data for visualization or modeling.
Progress0 / 4 steps
1
Create hourly temperature data
Create a pandas DataFrame called df with a datetime index from '2024-01-01 00:00' to '2024-01-02 23:00' at hourly intervals. Add a column called 'temperature' with these exact values: [30, 31, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16].
Data Analysis Python
Hint
Use pd.date_range to create the datetime index and pass it as the index when creating the DataFrame.
2
Set the resampling frequency
Create a variable called freq and set it to the string 'D' to represent daily frequency for resampling.
Data Analysis Python
Hint
Set freq to the string 'D' to represent daily frequency.
3
Resample to daily average temperature
Create a new DataFrame called daily_avg by resampling df using the freq variable and calculating the mean temperature for each day.
Data Analysis Python
Hint
Use df.resample(freq).mean() to get the daily average temperature.
4
Print the daily average temperature
Print the daily_avg DataFrame to display the average temperature for each day.
Data Analysis Python
Hint
Use print(daily_avg) to show the daily average temperatures.
Practice
(1/5)
1. What does resampling a time series data mean?
easy
A. Removing missing values
B. Sorting the data by values
C. Changing the time frequency of the data
D. Converting data to strings
Solution
Step 1: Understand the meaning of resampling
Resampling means changing how often data points appear in time, like from daily to monthly.
Step 2: Compare options to definition
Only Changing the time frequency of the data correctly describes changing the time frequency of data.
Final Answer:
Changing the time frequency of the data -> Option C
Quick Check:
Resampling = Changing frequency [OK]
Hint: Resampling changes how often data points appear [OK]
Common Mistakes:
Thinking resampling sorts data
Confusing resampling with cleaning data
Believing resampling changes data type
2. Which of the following is the correct syntax to resample a pandas DataFrame df to monthly frequency and take the mean?
easy
A. df.resample('month').mean()
B. df.resample('monthly').mean()
C. df.resample('M').average()
D. df.resample('M').mean()
Solution
Step 1: Identify the correct frequency string
Pandas uses 'M' for monthly frequency, not 'monthly' or 'month'.
Step 2: Check aggregation function name
The correct function is mean(), not average().
Final Answer:
df.resample('M').mean() -> Option D
Quick Check:
Use 'M' and mean() for monthly average [OK]
Hint: Use 'M' for month and mean() to aggregate [OK]
Common Mistakes:
Using 'monthly' or 'month' instead of 'M'
Using average() instead of mean()
Missing parentheses after mean
3. Given the following code, what will be the output?
B. Wrong frequency string 'W' for weekly resampling
C. DataFrame index is not a datetime index
D. Aggregation function 'mean' does not exist
Solution
Step 1: Check the resample syntax
The code uses mean without parentheses, so it returns a method, not the result.
Step 2: Verify frequency and index
'W' is correct for weekly, and the index is a datetime index, so no issues there.
Final Answer:
Missing parentheses after 'mean' function call -> Option A
Quick Check:
Call mean() with () to get result [OK]
Hint: Add () after mean to execute function [OK]
Common Mistakes:
Forgetting parentheses on aggregation functions
Thinking 'W' is invalid frequency
Not setting datetime index before resampling
5. You have minute-level temperature data in a DataFrame df indexed by datetime. You want to resample it to hourly data but only keep hours where the average temperature is above 20°C. Which code snippet correctly does this?
hard
A. df.resample('H').mean()[lambda x: x['temperature'] > 20]
B. df.resample('H').mean().query('temperature > 20')
C. df.resample('H').mean()[df.resample('H').mean() > 20]
D. df.resample('H').mean().filter(lambda x: x > 20)
Solution
Step 1: Resample to hourly and calculate mean
Use df.resample('H').mean() to get hourly averages.
Step 2: Filter rows where temperature > 20
Using .query('temperature > 20') filters rows correctly by the 'temperature' column.
Final Answer:
df.resample('H').mean().query('temperature > 20') -> Option B
Quick Check:
Use query() to filter by column condition [OK]
Hint: Use query() after resample mean to filter rows [OK]
Common Mistakes:
Applying boolean filter on DataFrame without column name
Using filter() incorrectly for row filtering
Repeating resample twice causing inefficiency
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.