What does the resample method do?

QuestionsQuestions8 SkillsProHandling Time Series DataAug, 06 2025
0190

The resample method in pandas is used to change the frequency of time series data. It allows you to aggregate or transform data over a specified time period, such as converting daily data to monthly data, hourly data to daily data, etc.

Key Features of resample:

  1. Frequency Specification: You can specify the new frequency using a string alias (e.g., 'M' for month-end, 'D' for day, 'H' for hour).

  2. Aggregation Functions: After resampling, you can apply aggregation functions such as mean(), sum(), max(), min(), etc., to summarize the data.

  3. Time Series Analysis: It is particularly useful for time series analysis, allowing you to analyze trends over different time intervals.

Example Usage:

Here’s an example of how to use the resample method:

import pandas as pd

# Sample time series data
date_rng = pd.date_range(start='2023-01-01', end='2023-01-10', freq='D')
data = {'value': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
df = pd.DataFrame(data, index=date_rng)

# Resample to get the monthly maximum value
monthly_max = df.resample('M').max()

print(monthly_max)

In this example:

  • A DataFrame is created with daily frequency data.
  • The resample('M') method is used to change the frequency to monthly, and max() is applied to get the maximum value for each month.

The resulting DataFrame will contain the maximum values for each month based on the original daily data.

0 Comments

no data
Be the first to share your comment!