0
0
PandasHow-ToBeginner · 3 min read

How to Resample Time Series Data in pandas: Simple Guide

Use the resample() method on a pandas DataFrame or Series with a datetime index to change the frequency of time series data. Specify the new frequency like 'D' for daily or 'M' for monthly, then apply an aggregation function like mean() to summarize the data.
📐

Syntax

The basic syntax for resampling time series data in pandas is:

  • data.resample(rule).aggregation()

Where:

  • data is a pandas DataFrame or Series with a datetime index.
  • rule is a string representing the new frequency (e.g., 'D' for daily, 'M' for monthly, 'H' for hourly).
  • aggregation() is a function like mean(), sum(), or count() to combine values in each new time bin.
python
data.resample('frequency').aggregation()
💻

Example

This example shows how to resample hourly data to daily frequency by taking the mean of values for each day.

python
import pandas as pd
import numpy as np

# Create hourly time series data
rng = pd.date_range('2024-01-01', periods=24*3, freq='H')
data = pd.Series(np.random.randint(0, 100, size=len(rng)), index=rng)

# Resample to daily frequency and calculate mean
daily_data = data.resample('D').mean()

print(daily_data)
Output
2024-01-01 48.5 2024-01-02 50.0 2024-01-03 49.0 Freq: D, dtype: float64
⚠️

Common Pitfalls

Common mistakes when resampling include:

  • Not having a datetime index before calling resample(). The index must be datetime type.
  • Forgetting to apply an aggregation function after resampling, which returns a Resampler object instead of data.
  • Using incorrect frequency strings or typos in the rule parameter.

Example of a wrong and right way:

python
# Wrong: no aggregation function
# data.resample('D')  # returns Resampler object, no data

# Right: apply aggregation
# data.resample('D').sum()  # returns summed daily data
📊

Quick Reference

Frequency StringMeaningExample
'S'Second2024-01-01 00:00:01
'T' or 'min'Minute2024-01-01 00:01:00
'H'Hour2024-01-01 01:00:00
'D'Day2024-01-01
'W'Week2024-01-07
'M'Month end2024-01-31
'Q'Quarter end2024-03-31
'A' or 'Y'Year end2024-12-31

Key Takeaways

Always ensure your data has a datetime index before resampling.
Use resample() with a frequency string and follow it with an aggregation function like mean() or sum().
Common frequency strings include 'D' for daily, 'M' for monthly, and 'H' for hourly.
Without an aggregation function, resample() returns a Resampler object, not data.
Check for typos in frequency strings to avoid errors.