Skip to content

Repository files navigation

Window ops

This library is intended to be used as an alternative to pd.Series.rolling and pd.Series.expanding to gain a speedup by using numba optimized functions operating on numpy arrays. There are also online classes for more efficient updates of window statistics.

Install

PyPI

pip install window-ops

conda

conda install -c conda-forge window-ops

How to use

Transformations

For a transformations n_samples -> n_samples you can use [seasonal_](rolling|expanding)_(mean|max|min|std) on an array.

Benchmarks

pd.__version__
'1.3.5'
n_samples=10_000# array sizewindow_size=8# for rolling operationsseason_length=7# for seasonal operationsexecute_times=10# number of times each function will be executed

Average times in milliseconds.

times.applymap('{:.2f}'.format)
window_opspandas
rolling_mean0.030.43
rolling_max0.140.57
rolling_min0.140.58
rolling_std0.060.54
expanding_mean0.030.31
expanding_max0.050.76
expanding_min0.050.47
expanding_std0.090.41
seasonal_rolling_mean0.053.89
seasonal_rolling_max0.184.27
seasonal_rolling_min0.183.75
seasonal_rolling_std0.084.38
seasonal_expanding_mean0.043.18
seasonal_expanding_max0.063.29
seasonal_expanding_min0.063.28
seasonal_expanding_std0.123.89
speedups=times['pandas'] /times['window_ops']
speedups=speedups.to_frame('times faster')
speedups.applymap('{:.0f}'.format)
times faster
rolling_mean15
rolling_max4
rolling_min4
rolling_std9
expanding_mean12
expanding_max15
expanding_min9
expanding_std4
seasonal_rolling_mean77
seasonal_rolling_max23
seasonal_rolling_min21
seasonal_rolling_std52
seasonal_expanding_mean78
seasonal_expanding_max52
seasonal_expanding_min51
seasonal_expanding_std33

Online

If you have an array for which you want to compute a window statistic and then keep updating it as more samples come in you can use the classes in the window_ops.online module. They all have a fit_transform method which take the array and return the transformations defined above but also have an update method that take a single value and return the new statistic.

Benchmarks

Average time in milliseconds it takes to transform the array and perform 100 updates.

times.to_frame().applymap('{:.2f}'.format)
average time (ms)
RollingMean0.12
RollingMax0.23
RollingMin0.22
RollingStd0.32
ExpandingMean0.10
ExpandingMax0.07
ExpandingMin0.07
ExpandingStd0.17
SeasonalRollingMean0.28
SeasonalRollingMax0.35
SeasonalRollingMin0.38
SeasonalRollingStd0.42
SeasonalExpandingMean0.17
SeasonalExpandingMax0.14
SeasonalExpandingMin0.15
SeasonalExpandingStd0.23

About

Fast window operations

Topics

Resources

Contributing

Stars

44 stars

Watchers

2 watching

Forks

Releases

Contributors

Languages

Generated from fastai/nbdev_template