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

single

bookResampling and Frequency Conversion

Sveip for å vise menyen

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.

Oppgave

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 6
single

single

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

some-alt