Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Resampling and Frequency Conversion | Section
Mastering Time Series Fundamentals
Section 1. Chapter 6
single

single

bookResampling and Frequency Conversion

Swipe to show menu

123456789
import pandas as pd # Create a simple daily time series dates = pd.date_range("2024-01-01", periods=90, freq="D") values = pd.Series(range(90), index=dates) # Resample to monthly frequency, taking the average for each month monthly_avg = values.resample("ME").mean() print(monthly_avg)
copy

When working with time series data in pandas, the resample method allows you to convert your data from one frequency to another, such as from daily to monthly or weekly. This is useful for summarizing, aggregating, or aligning your data to a different time scale.

The resample method works similarly to groupby, but it is specifically designed for time series data with a DatetimeIndex. You specify a new frequency using a frequency string, such as "ME" for month-end, "W" for week-end, or "D" for daily. After resampling, you apply an aggregation function, such as .mean(), .sum(), .max(), or .min(), to summarize the values within each new period.

In the code sample above, the daily series is resampled to monthly frequency using "ME", and the .mean() function calculates the average value for each month. You can use other frequency strings to resample to different periods, and choose the aggregation function that best fits your analysis needs.

Task

Swipe to start coding

Resample a daily time series to weekly frequency and compute the sum for each week.

  • Use the resample method on the input series with a frequency of "W".
  • Aggregate the values within each week using the sum function.
  • Return the resulting weekly series.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 6
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt