Python | Pandas dataframe.resample()
Resampling is the process of changing the frequency of time-indexed data for example, converting daily data into weekly, monthly, or quarterly intervals. In Pandas, resample() is used to perform such time-based grouping and aggregation.
During resampling, functions like .mean() are used to combine all values within each new time interval and return their average. This helps summarize time-grouped data efficiently.
Syntax
The syntax below shows how the resample() function is applied on a DataFrame or Series:
DataFrame.resample(rule, axis=0, closed=None, label=None, on=None, level=None)
Parameters:
- rule: Frequency string (e.g., 'D', 'W', 'ME', 'QE') defining target time interval.
- axis (optional): Axis to resample. Default is 0 (index).
- closed (optional): Interval side that is closed - 'left' or 'right'.
- label (optional): Whether labels represent left or right interval boundary.
- on (optional): Column to use instead of index for resampling. Must be datetime-like.
- level (optional): For MultiIndex - datetime like level to use for resampling.
Examples
We will use this dataset in all examples below.
import pandas as pd
df = pd.read_csv("apple.csv")
print(df.head())
Output

To download the file "apple.csv" used in this article, click here
Example 1: This example groups daily closing prices into monthly buckets and calculates the monthly average using "ME" (Month-End).
import pandas as pd
df = pd.read_csv("apple.csv", parse_dates=["date"], dayfirst=True, index_col="date")
res = df["close"].resample("ME").mean()
print(res)
Output

Explanation:
- df["close"] selects the closing price column
- resample("ME") groups data by month-end
- mean() computes average closing price for each month
Example 2: This example resamples opening prices into weekly groups and computes the average opening price per week.
import pandas as pd
df = pd.read_csv("apple.csv", parse_dates=["date"], dayfirst=True, index_col="date")
res = df["open"].resample("W").mean()
print(res)
Output

Explanation:
- .resample("W") groups data by week
- .mean() returns average opening price per week
Example 3: This example converts daily opening prices into quarterly groups and calculates the quarterly averages using "QE" (Quarter-End).
import pandas as pd
df = pd.read_csv("apple.csv", parse_dates=["date"], dayfirst=True, index_col="date")
res = df["open"].resample("QE").mean()
print(res.head(10))
Output

Explanation:
- resample("QE") forms quarterly time blocks (quarter-end)
- mean() computes the average opening price for each quarter