0
0
Data Analysis Pythondata~5 mins

Resampling time series in Data Analysis Python

Choose your learning style9 modes available
Introduction

Resampling helps change the frequency of time series data. It lets you see data in bigger or smaller time steps.

You have daily sales data but want to see monthly totals.
You want to convert minute-level temperature readings to hourly averages.
You need to fill missing dates in your data with estimated values.
You want to downsample high-frequency stock prices to daily closing prices.
Syntax
Data Analysis Python
df.resample('rule').aggregation_function()

rule is a string like 'D' for day, 'M' for month, 'H' for hour.

You must use an aggregation function like mean(), sum(), or count() after resampling.

Examples
Sum values for each month.
Data Analysis Python
df.resample('M').sum()
Calculate average for each hour.
Data Analysis Python
df.resample('H').mean()
Find maximum value for each week.
Data Analysis Python
df.resample('W').max()
Sample Program

This code creates daily data from June 1 to June 10 with values 1 to 10. Then it resamples the data by week and sums the values in each week.

Data Analysis Python
import pandas as pd
import numpy as np

# Create sample daily data for 10 days
rng = pd.date_range('2024-06-01', periods=10, freq='D')
data = np.arange(10) + 1  # values 1 to 10
df = pd.DataFrame({'value': data}, index=rng)

# Resample to weekly sum
weekly_sum = df.resample('W').sum()

print('Original daily data:')
print(df)
print('\nWeekly sum data:')
print(weekly_sum)
OutputSuccess
Important Notes

Make sure your DataFrame index is a datetime type before resampling.

Resampling up (to higher frequency) usually needs a method like ffill() or bfill() to fill new rows.

Summary

Resampling changes the time frequency of your data.

Use a string rule like 'D', 'W', 'M' to set the new frequency.

Always apply an aggregation function after resampling.