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
