← Back to Pandas Course | Chapter 10: Time Series | Lesson 3 of 7

Resampling

resample changes the time frequency of data, such as turning daily numbers into weekly totals.

In this page:

  1. Resampling
Syntax
python
df.resample("W").sum()
df.resample("M")["column"].mean()

Resampling

resample("W") groups a datetime-indexed series into weekly bins and needs an aggregation like sum or mean. Downsampling reduces frequency, while upsampling increases it and creates gaps to fill with ffill or interpolate.

Common rules include "D", "W" and "2D".

Note: resample is groupby for time.

Example: Resampling

python
import pandas as pd

idx = pd.date_range("2024-01-01", periods=6, freq="D")
s = pd.Series([1, 2, 3, 4, 5, 6], index=idx)
print(s.resample("2D").sum())
print(s.resample("W").mean())

# Output:
# 2024-01-01     3
# 2024-01-03     7
# 2024-01-05    11
# Freq: 2D, dtype: int64
# 2024-01-07    3.5
# Freq: W-SUN, dtype: float64
Related Topics
Common Mistakes
  1. Forgetting an aggregation
  2. Resampling without a DatetimeIndex
  3. Not filling gaps when upsampling
Chapter Summary
  • resample regroups by time frequency
  • It needs a datetime index
  • Downsampling aggregates
  • Upsampling needs filling
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.