Bird
Raised Fist0
Data Analysis Pythondata~10 mins

Resampling time series in Data Analysis Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to resample the time series data to daily frequency.

Data Analysis Python
daily_data = data.[1]('D').mean()
Drag options to blanks, or click blank then click option'
Asort
Bgroupby
Cpivot
Dresample
Attempts:
3 left
💡 Hint
Common Mistakes
Using groupby instead of resample
Trying to use pivot which is for reshaping data
Using sort which only orders data
2fill in blank
medium

Complete the code to resample the data to monthly frequency and sum the values.

Data Analysis Python
monthly_sum = data.[1]('M').sum()
Drag options to blanks, or click blank then click option'
Aresample
Baggregate
Cfilter
Dgroupby
Attempts:
3 left
💡 Hint
Common Mistakes
Using groupby which does not work directly on time index
Using aggregate without resampling first
3fill in blank
hard

Fix the error in the code to resample data to hourly frequency and take the mean.

Data Analysis Python
hourly_mean = data.[1]('H').mean()
Drag options to blanks, or click blank then click option'
Agroupby
Brolling
Cresample
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using groupby which does not handle time frequency
Using rolling which is for moving averages
4fill in blank
hard

Fill both blanks to create a dictionary of weekly max values for each column in the DataFrame.

Data Analysis Python
weekly_max = {col: data[col].[1]('W').[2]() for col in data.columns}
Drag options to blanks, or click blank then click option'
Aresample
Bgroupby
Cmax
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using groupby instead of resample
Using mean instead of max
5fill in blank
hard

Fill all three blanks to create a dictionary of resampled data with minimum values monthly for each column.

Data Analysis Python
monthly_min = [1]: data[[2]].[3]('M').min() for [1] in data.columns
Drag options to blanks, or click blank then click option'
Acol
Cresample
Dgroupby
Attempts:
3 left
💡 Hint
Common Mistakes
Using groupby instead of resample
Using inconsistent variable names in comprehension

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

  1. Step 1: Understand the meaning of resampling

    Resampling means changing how often data points appear in time, like from daily to monthly.
  2. Step 2: Compare options to definition

    Only Changing the time frequency of the data correctly describes changing the time frequency of data.
  3. Final Answer:

    Changing the time frequency of the data -> Option C
  4. 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

  1. Step 1: Identify the correct frequency string

    Pandas uses 'M' for monthly frequency, not 'monthly' or 'month'.
  2. Step 2: Check aggregation function name

    The correct function is mean(), not average().
  3. Final Answer:

    df.resample('M').mean() -> Option D
  4. 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?
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()
print(resampled)
medium
A. 2023-01-01 30 2023-01-03 70
B. 2023-01-01 10 2023-01-03 30 2023-01-05 40
C. 2023-01-01 30 2023-01-03 40
D. 2023-01-01 10 2023-01-02 20 2023-01-03 30 2023-01-04 40

Solution

  1. Step 1: Understand the original data and frequency

    Data has values 10,20,30,40 on consecutive days from 2023-01-01 to 2023-01-04.
  2. Step 2: Resample with '2D' and sum

    Resampling groups every 2 days: (Jan 1+2) and (Jan 3+4). Sums are 10+20=30 and 30+40=70.
  3. Final Answer:

    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 starting from first date [OK]
Common Mistakes:
  • Summing only one day instead of two
  • Confusing sum with mean
  • Misreading the date index
4. What is wrong with this code snippet?
df = pd.DataFrame({'value': [1, 2, 3]}, index=pd.date_range('2023-01-01', periods=3))
resampled = df.resample('W').mean
medium
A. Missing parentheses after 'mean' function call
B. Wrong frequency string 'W' for weekly resampling
C. DataFrame index is not a datetime index
D. Aggregation function 'mean' does not exist

Solution

  1. Step 1: Check the resample syntax

    The code uses mean without parentheses, so it returns a method, not the result.
  2. Step 2: Verify frequency and index

    'W' is correct for weekly, and the index is a datetime index, so no issues there.
  3. Final Answer:

    Missing parentheses after 'mean' function call -> Option A
  4. 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

  1. Step 1: Resample to hourly and calculate mean

    Use df.resample('H').mean() to get hourly averages.
  2. Step 2: Filter rows where temperature > 20

    Using .query('temperature > 20') filters rows correctly by the 'temperature' column.
  3. Final Answer:

    df.resample('H').mean().query('temperature > 20') -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard