resample()
noun
say it: “ree-SAM-pul” /riˈsæmpəl/
A pandas method that groups rows with dates into regular periods, such as weeks or months, and summarizes each period, empty ones included.
What does resample() do in pandas?
resample() is a pandas method that cuts a date index into periods, such as weeks or months, and summarizes each
one. Unlike a grouping, it keeps every period from the first to the last, so a week with no orders shows up as 0
instead of disappearing. "W" labels each week by the Sunday it ends on.
import pandas as pd
orders = pd.DataFrame({"ordered_at": pd.to_datetime(["2026-06-01", "2026-06-02", "2026-06-16"]),
"total": [20.0, 15.0, 30.0]})
weekly = orders.set_index("ordered_at")["total"].resample("W").sum()
print(weekly)
# ordered_at
# 2026-06-07 35.0
# 2026-06-14 0.0
# 2026-06-21 30.0
In a sentence
- “She used resample() to turn hourly readings into daily averages.”
- “resample() totaled the orders week by week, with 0 for the week the shop was closed.”
Level: Freelancer · More Python terms at this level
Updated