liveBook · Manning

Pandas.DataFrame.resample()

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A pandas operation that groups time-indexed data into regular temporal intervals and applies aggregation or other computations to each interval.

Treat resample as groupby for time series: place datetime values on the index, choose a time interval, and aggregate each interval. Codes such as 1D, 1M, 1W, 2M, and 1Y select daily, monthly, weekly, bimonthly, and annual periods. section 10.10 section 10.14

Keep these output behaviors in mind:

  • Returns a data frame whose index uses the end of each period, such as December 31 for annual results. section 10.10
  • Includes periods across the start-to-finish range even when a period is partial or has no observations; empty periods can produce NaN values. section 10.10 section 10.14
  • Supports follow-up operations such as sort_values, dropna, and comparisons between consecutive periods. section 10.12 section 10.14

Annual oil-price averages

With the WTI data indexed by parsed Date values, run df.resample('1Y').mean() to calculate each year's mean price. The result includes annual periods from the first through the last year, including years represented only partially in the data. section 10.10

Finding the highest-tipping days

After setting taxi pickup time as the index, calculate df.resample('1D')['tip_percentage'].mean(), sort the daily means, and inspect the top ten dates. Resampling monthly and applying dropna then compares January and July without retaining empty months. section 10.14

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains interval codes, period-end indexes, partial periods, empty-period NaN values, and chained analysis operations.
Examples
Uses WTI oil prices for annual means and taxi tip percentages for daily rankings and month comparisons.
What it assumes
Assumes datetime values are available as an index and teaches interval selection through compact frequency codes.
What you can do after
Apply it via pandas to calculate, sort, compare, and investigate time-based summaries.
How it connects
Connects datetime indexes with groupby, aggregation, sorting, and dropna.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.