Resampling—Aggregation and Other Parameters
Understand the other capabilities of resampling such as performing group-by aggregations and utilizing parameters like origin and offset.
Introduction
Now that we've learned the general use of the resample()
method for upsampling and downsampling, let's explore its other capabilities, such as aggregation functions and origin definition. We’ll again use the truncated dataset version of Spain’s 2018 hourly energy demand data.
Preview of Spain 2018 Hourly Energy Demand Data
Time | Generation Fossil Gas | Generation Fossil Hard Coal |
2018-01-01 00:00:00 | 3471 | 996 |
2018-01-01 01:00:00 | 3269 | 959 |
2018-01-01 02:00:00 | 3541 | 1014 |
2018-01-01 03:00:00 | 3450 | 1043 |
2018-01-01 04:00:00 | 3318 | 1063 |
Groupby aggregation functions
Given that resample()
is essentially a time-based groupby operation coupled with an aggregation function, we can expect the common groupby functions to be applicable too. For example, the following code demonstrates some of the common aggregation functions that can be applied to each interval group for a downsampling example:
# Meanprint('--------------- Mean ---------------')print(df.resample(rule='3H').mean().head(3))# Sumprint('\n--------------- Sum ---------------')print(df.resample(rule='3H').sum().head(3))# Standard deviation (std)print('\n--------------- STD ---------------')print(df.resample(rule='3H').std().head(3))# Standard error of the meanprint('\n--------------- SEM ---------------')print(df.resample(rule='3H').sem().head(3))# Maximumprint('\n--------------- Max ---------------')print(df.resample(rule='3H').max().head(3))# Minimumprint('\n--------------- Min ---------------')print(df.resample(rule='3H').min().head(3))# Firstprint('\n--------------- First ---------------')print(df.resample(rule='3H').first().head(3))# Lastprint('\n--------------- Last ---------------')print(df.resample(rule='3H').last().head(3))# Open, High, Low, Closeprint('\n--------------- OHLC ---------------')print(df.resample(rule='3H').ohlc().head(3))
While it’s common to apply a single aggregation function like ...
Get hands-on with 1400+ tech skills courses.