Pandasdrill

Time series and windows Updated November 2026

resample

df.resample("ME").sum() groups rows on a DatetimeIndex into calendar months and sums each one; any frequency alias works ("D", "W", "QE", "YE"). Use on="date_col" when the dates are in a column, or Grouper inside groupby to combine it with other keys.

Syntax

df.resample(rule, on=None, closed=None, label=None).agg_method()
PartWhat it is
ruleFrequency alias: "D" day, "W" week, "ME" month end, "MS" month start, "QE" quarter end, "YE" year end, "h" hour
onName of a datetime column to use instead of the index

Examples

import pandas as pd

df = pd.DataFrame({"date": pd.to_datetime(["2024-01-05", "2024-01-20", "2024-02-03", "2024-02-28"]),
                   "sales": [100, 50, 80, 20]})
df.resample("ME", on="date")["sales"].sum()

Result: date 2024-01-31 150 2024-02-29 100 Freq: ME, Name: sales, dtype: int64

df.set_index("date")["sales"].resample("MS").agg(["sum", "count"])

Result: sum count date 2024-01-01 150 2 2024-02-01 100 2

MS labels each bucket with the first day of the month.

s = pd.Series([1, 3], index=pd.to_datetime(["2024-01-01", "2024-01-03"]))
s.resample("D").ffill()

Result: 2024-01-01 1 2024-01-02 1 2024-01-03 3 Freq: D, dtype: int64

Upsampling to daily and forward filling the gap.

Common mistakes

  • TypeError: Only valid with DatetimeIndex: set the date column as the index, or pass on="date".
  • FutureWarning: 'M' is deprecated: pandas 2.2 renamed the aliases to "ME", "QE", "YE" and lowercase "h", "min".
  • Empty months show NaN (for mean) or 0 (for sum) because resample fills in every period; filter them out if unwanted.

Practice resample

These drills check your answer on live data, free, no account needed.

  • Monthly totals with resampleTotal `amount` per calendar month with `resample`, labeling each month by its first day (rule "MS"). Months with no transactions must appear with a total of 0.

Questions

resample or groupby with dt.month?

resample creates a proper time axis including empty periods and labels them with dates; groupby(df.date.dt.to_period("M")) is fine for simple monthly totals.

How do I resample per customer?

df.groupby(["customer", pd.Grouper(key="date", freq="ME")])["sales"].sum().

How do I resample to the start of the week?

Use "W-MON" with label="left", closed="left" so each bucket is labeled by its Monday.

Related

  • pd.to_datetimeHow to parse dates in pandas with pd.to_datetime: formats, day-first dates, coercing bad values, and extracting year, month and weekday with the dt accessor.
  • rollingHow to compute a moving average in pandas with rolling: window size, min_periods to avoid leading NaN, centered windows, rolling sum and std, time windows.
  • groupbyHow groupby works in pandas: group by one or more columns, apply sum, mean, count or size, get a flat table with as_index=False, and iterate over groups.
  • shift and diffHow to use shift and diff in pandas to get the previous or next row, compute the change and percent change between periods, and lag within groups.

Previous: rankNext: cut and qcut