Skip to main content

Naive implementations of window operations such as rolling and expanding.

Project description

Window ops

Naive and fast implementations of common window operations.

This library is intended to be used as a replacement to pd.rolling and pd.expanding to gain a speedup by operating on numpy arrays and avoiding input checks.

Install

pip install window_ops

How to use

Rolling

Simply use rolling_{op} with a one dimensional np.ndarray by specifying the window size and the minimum number of samples to compute the operation (by default min_samples equals window_size). The result will have min_samples - 1 np.nan's at the beggining of the array.

import numpy as np
import pandas as pd
from window_ops.rolling import *
from window_ops.expanding import *

np.random.seed(0)
y = np.random.rand(10)
window_size = 3
y
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ,
       0.64589411, 0.43758721, 0.891773  , 0.96366276, 0.38344152])
rolling_mean(y, window_size=window_size)
array([       nan,        nan, 0.62225544, 0.62094533, 0.5237671 ,
       0.53814405, 0.5023787 , 0.6584181 , 0.764341  , 0.7462924 ],
      dtype=float32)
ys = pd.Series(y)
ys.rolling(window_size).mean().values
array([       nan,        nan, 0.62225542, 0.62094531, 0.52376712,
       0.53814403, 0.50237871, 0.65841811, 0.76434099, 0.74629243])
y = np.random.rand(1_000)

%timeit rolling_mean(y, window_size=8)
2.63 µs ± 104 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
ys = pd.Series(y)

%timeit ys.rolling(8).mean()
339 µs ± 16.8 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Expanding

Simply use expanding_{op} with a one dimensional np.ndarray. For expanding_std the first value in the output array is np.nan, for all the other operations a full array is returned.

y = np.random.rand(10)
y
array([0.58231973, 0.10747257, 0.2875445 , 0.45670363, 0.02095007,
       0.41161551, 0.48945864, 0.24367788, 0.588639  , 0.75324012])
expanding_mean(y)
array([0.58231974, 0.34489614, 0.32577893, 0.3585101 , 0.2909981 ,
       0.311101  , 0.33658066, 0.3249678 , 0.35426462, 0.39416218],
      dtype=float32)
ys = pd.Series(y)
ys.expanding().mean().values
array([0.58231973, 0.34489615, 0.32577893, 0.35851011, 0.2909981 ,
       0.311101  , 0.33658066, 0.32496782, 0.35426461, 0.39416216])
y = np.random.rand(1_000)

%timeit expanding_mean(y)
1.92 µs ± 58.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit ys.expanding().mean()
294 µs ± 19.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

window_ops-0.0.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

window_ops-0.0.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file window_ops-0.0.2.tar.gz.

File metadata

  • Download URL: window_ops-0.0.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.6

File hashes

Hashes for window_ops-0.0.2.tar.gz
Algorithm Hash digest
SHA256 b46093aff9f2f09756da8bd51d5eeb78e5c94e5ab98ca9b6583909a106df4b69
MD5 4d22433077a3e1dbfff245891969406a
BLAKE2b-256 6001738236ce17a19cb070d06c6121dc282c5ba9ec79cd461c7731544a1cc303

See more details on using hashes here.

File details

Details for the file window_ops-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: window_ops-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/49.6.0.post20200814 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.7.6

File hashes

Hashes for window_ops-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1d51e34dbdd7b35112165017cdbc38193cf224a2c5a74dc707e97bc07b49deb3
MD5 c8bc30dce87ab29a125475b01d953936
BLAKE2b-256 a8d054731ec8a2c1bcdb71c963054a9aa55d3c6840b07557e79cebab077eb4ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page