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
Sektion 1. Kapitel 6
single

single

bookResampling and Frequency Conversion

Stryg for at vise menuen

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.

Opgave

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 desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 6
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt