This notebook explains how to calculate the energy of time series data in pandas
.
This notebook will use gold and silver price data from rdatasets
for this tutorial
The documentation for each package used in this tutorial is linked below:
import statsmodels.api as sm
import pandas as pd
The data is from rdatasets
imported using the Python package statsmodels
.
df = sm.datasets.get_rdataset('GoldSilver', 'AER').data.reset_index().rename(columns={'index': 'date'})
df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 9132 entries, 0 to 9131 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 date 9132 non-null object 1 gold 9132 non-null float64 2 silver 9132 non-null float64 dtypes: float64(2), object(1) memory usage: 214.2+ KB
df['date'] = pd.to_datetime(df.date)
The pandas
function rolling
can be used to create aggregations on windows of specific lengths. Here, an aggregate of the daily gold and silver price data will be created covering the primary week.
First, a datetime index needs to be created from the date column.
df.set_index('date', inplace=True)
Pandas aggregation will require a function that can be called. The energy of a series is the sum of the squared magnatude of the signal.
def energy(series):
return sum(series**2)
If, instead of an offset ('7D' representing 7 days), a number is used, it will just use the prior number of observations.
weekly_resample = df.rolling('7D')
aggregated_df = weekly_resample.agg(['mean', energy])
aggregated_df.columns = ['_'.join(col).strip() + '_week' for col in aggregated_df.columns.values]
aggregated_df.head(20)
gold_mean_week | gold_energy_week | silver_mean_week | silver_energy_week | |
---|---|---|---|---|
date | ||||
1977-12-30 | 100.000 | 10000.0000 | 223.420 | 49916.4964 |
1978-01-02 | 100.000 | 20000.0000 | 223.420 | 99832.9928 |
1978-01-03 | 100.000 | 30000.0000 | 225.560 | 152659.4184 |
1978-01-04 | 100.000 | 40000.0000 | 225.315 | 203095.5948 |
1978-01-05 | 100.000 | 50000.0000 | 225.850 | 255075.0349 |
1978-01-06 | 100.000 | 50000.0000 | 226.604 | 256773.8346 |
1978-01-09 | 100.246 | 50247.5129 | 227.844 | 259582.6826 |
1978-01-10 | 100.436 | 50438.4154 | 227.670 | 259183.5179 |
1978-01-11 | 100.886 | 50893.4779 | 228.998 | 262210.0299 |
1978-01-12 | 101.062 | 51070.2523 | 228.978 | 262164.4419 |
1978-01-13 | 101.586 | 51601.1167 | 230.050 | 264628.6483 |
1978-01-16 | 101.958 | 51981.1519 | 230.624 | 265954.9040 |
1978-01-17 | 102.124 | 52149.4178 | 230.866 | 266510.4755 |
1978-01-18 | 101.848 | 51869.1122 | 230.214 | 265013.5487 |
1978-01-19 | 102.012 | 52035.2278 | 230.412 | 265465.7510 |
1978-01-20 | 101.832 | 51851.3218 | 229.686 | 263790.6149 |
1978-01-23 | 101.938 | 51960.8781 | 229.988 | 264495.0148 |
1978-01-24 | 102.300 | 52332.5978 | 230.678 | 266095.1593 |
1978-01-25 | 102.836 | 52880.4434 | 231.986 | 269119.6477 |
1978-01-26 | 103.152 | 53204.3118 | 232.508 | 270321.2134 |