Skip to main content

Polars Time Series Extension

Project description

Polars Time Series Extension

A high-performance time series analysis toolkit for Polars, with Rust-powered distance metrics and Python analytics.


Documentation: https://drumtorben.github.io/polars-ts

Source Code: https://github.com/drumtorben/polars-ts

PyPI: https://pypi.org/project/polars-timeseries


Features

Distance Metrics (Rust, parallelized via Rayon)

Metric Function Key Parameters
Dynamic Time Warping compute_pairwise_dtw method: standard, sakoe_chiba, itakura, fast
Derivative DTW compute_pairwise_ddtw Shape-sensitive comparison
Weighted DTW compute_pairwise_wdtw g: weight sharpness
Move-Split-Merge compute_pairwise_msm c: move cost
Edit Distance (Real Penalty) compute_pairwise_erp g: gap value
Longest Common Subsequence compute_pairwise_lcss epsilon: matching threshold
Time Warp Edit Distance compute_pairwise_twe nu: stiffness, lambda_: deletion cost
Multivariate DTW compute_pairwise_dtw_multi metric: manhattan, euclidean
Multivariate MSM compute_pairwise_msm_multi c: move cost

Trend & Changepoint Detection (Rust)

  • Mann-Kendall test — non-parametric trend detection
  • Sen's slope — robust trend magnitude estimation
  • CUSUM — cumulative sum changepoint detection

Decomposition (Python)

  • Seasonal decomposition — additive or multiplicative (classical)
  • Fourier decomposition — harmonic decomposition with configurable frequencies
  • Decomposition features — trend/seasonal strength extraction (simple or MSTL)
  • Anomaly flagging — residual-based anomaly detection from any decomposition

Forecasting

  • SCUM — ensemble model combining AutoARIMA, AutoETS, AutoCES, and DynamicOptimizedTheta
  • Kaboudan metric — model robustness evaluation via block-shuffle backtesting

Installation

pip install polars-timeseries

Optional dependencies for specific features:

pip install "polars-timeseries[forecast]"      # Kaboudan metric, SCUM model
pip install "polars-timeseries[decomposition]"  # Fourier decomposition
pip install "polars-timeseries[all]"            # Everything

Requires Python 3.12+ and Polars 1.30+.

Quick Start

Pairwise DTW distance

import polars as pl
import polars_ts as pts

df = pl.DataFrame({
    "unique_id": ["A"] * 5 + ["B"] * 5,
    "y": [1.0, 2.0, 3.0, 2.0, 1.0,
          1.0, 3.0, 5.0, 3.0, 1.0],
})

# Standard DTW
result = pts.compute_pairwise_dtw(df, df)

# With Sakoe-Chiba band constraint
result = pts.compute_pairwise_dtw(df, df, method="sakoe_chiba", param=2)

K-Medoids clustering

import polars as pl
import polars_ts as pts

df = pl.DataFrame({
    "group": ["A"] * 10 + ["B"] * 10,
    "y": list(range(10)) + [10 - x for x in range(10)],
})

    "unique_id": ["A"] * 5 + ["B"] * 5 + ["C"] * 5,
    "y": [1.0, 1.0, 1.0, 1.0, 1.0,   # flat low
          5.0, 5.0, 5.0, 5.0, 5.0,   # flat high
          1.0, 5.0, 1.0, 5.0, 1.0],  # oscillating
})

clusters = pts.kmedoids(df, k=3, method="dtw")
# Returns: DataFrame with [unique_id, cluster]

K-NN classification

import polars as pl
import polars_ts as pts

train = pl.DataFrame({
    "unique_id": ["t1"] * 4 + ["t2"] * 4,
    "y": [1.0, 1.0, 1.0, 1.0, 5.0, 5.0, 5.0, 5.0],
    "label": ["low"] * 4 + ["high"] * 4,
})
test = pl.DataFrame({
    "unique_id": ["x1"] * 4,
    "y": [1.0, 1.0, 1.0, 1.1],
})

predictions = pts.knn_classify(train, test, k=1, method="dtw")
# Returns: DataFrame with [unique_id, predicted_label]

Mann-Kendall trend test

import polars as pl
import polars_ts as pts

df = pl.DataFrame({
    "group": ["A"] * 10 + ["B"] * 10,
    "y": list(range(10)) + [10 - x for x in range(10)],
})

result = df.group_by("group").agg(
    pts.mann_kendall(pl.col("y")).alias("trend"),
    pts.sens_slope(pl.col("y")).alias("slope"),
)

Seasonal decomposition

import polars as pl
import polars_ts as pts

df = pl.DataFrame({
    "unique_id": ["A"] * 48,
    "ds": list(range(48)),
    "y": [10 + 5 * (i % 12 > 5) + 0.5 * i for i in range(48)],
})


### Seasonal decomposition

```python
import polars as pl
import polars_ts as pts

df = pl.DataFrame({
    "unique_id": ["A"] * 48,
    "ds": list(range(48)),
    "y": [10 + 5 * (i % 12 > 5) + 0.5 * i for i in range(48)],
})

result = pts.seasonal_decomposition(df, freq=12, method="additive")

CUSUM changepoint detection

import polars as pl
import polars_ts as pts

df = pl.DataFrame({
    "unique_id": ["A"] * 20,
    "y": [1.0] * 10 + [5.0] * 10,
})

result = pts.cusum(df)

Kaboudan metric

import polars as pl
from statsforecast import StatsForecast
from statsforecast.models import AutoETS, OptimizedTheta
import polars_ts as pts  # noqa

df = (
    pl.scan_parquet("https://datasets-nixtla.s3.amazonaws.com/m4-hourly.parquet")
    .filter(pl.col("unique_id").is_in(["H1", "H2", "H3"]))
    .collect()
)

sf = StatsForecast(
    models=[OptimizedTheta(season_length=24), AutoETS(season_length=24)],
    freq=1, n_jobs=-1,
)

res = df.pts.kaboudan(sf, block_size=200, backtesting_start=0.5, n_folds=10)

Development

git clone https://github.com/drumtorben/polars-ts.git
cd polars-ts
uv sync
uv pip install -e .
uv run pytest

License

MIT

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

polars_timeseries-0.5.0.tar.gz (328.1 kB view details)

Uploaded Source

Built Distributions

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

polars_timeseries-0.5.0-cp312-abi3-win_amd64.whl (15.9 MB view details)

Uploaded CPython 3.12+Windows x86-64

polars_timeseries-0.5.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.9 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

polars_timeseries-0.5.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (17.2 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

polars_timeseries-0.5.0-cp312-abi3-macosx_11_0_arm64.whl (15.7 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

polars_timeseries-0.5.0-cp312-abi3-macosx_10_12_x86_64.whl (16.4 MB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

File details

Details for the file polars_timeseries-0.5.0.tar.gz.

File metadata

  • Download URL: polars_timeseries-0.5.0.tar.gz
  • Upload date:
  • Size: 328.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for polars_timeseries-0.5.0.tar.gz
Algorithm Hash digest
SHA256 fc3e648740498f8185b6aaf18705190b72db453a0f782bbc2098ac48c8b36b1c
MD5 580fbd872cf1f291b81d5e56b93564cd
BLAKE2b-256 0169c14b3bf8756a8a3d630e622c057bbaf0bc9bd396716aae753b10b8479659

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.5.0.tar.gz:

Publisher: release.yml on drumtorben/polars-ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_timeseries-0.5.0-cp312-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.5.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 01c53bab21ac4f063752b69ebfd004f7af55a4f3593c51493075414a5da9d960
MD5 459d26c6f4a29f2c4f8ee093475636fb
BLAKE2b-256 0403e8daeae9ea1abf49402d958873e1dc99cbb30f8bfb4257e38e0d6fa4151b

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.5.0-cp312-abi3-win_amd64.whl:

Publisher: release.yml on drumtorben/polars-ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_timeseries-0.5.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.5.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 12eab34f85d50d06029c395abf49afe2cb4e63ffb5ed47cd5705927884992966
MD5 134ccc5876854edaaea046c6c0a591f2
BLAKE2b-256 ee88821eb194f569f4557d1d4925b0e44838898af9a625c77293c123135aae2a

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.5.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on drumtorben/polars-ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_timeseries-0.5.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.5.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a979b22acda8b8ed52bebbc4293612a07a9320af635bce3dcf91f46495fc6ec8
MD5 53927798b038444ad4953d6fa60742db
BLAKE2b-256 76cac6099f678b7071386933c82247c37f61bd1062050531b72123605e8d0061

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.5.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on drumtorben/polars-ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_timeseries-0.5.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.5.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6c730c3dc4c3d5bf440c7517b03f753d6109b59d734b8fa633f2b9f700c001a9
MD5 7ed2b05ed4276ee180ed50cafb9490ff
BLAKE2b-256 98cdf8133bc4c976ed38afc08147874ca43d15fcb143749a33a76400cd0349fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.5.0-cp312-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on drumtorben/polars-ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file polars_timeseries-0.5.0-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.5.0-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c7b74f8c611792a74d9de15e35667ce1f1980e9acd9f240536421bfd32474e94
MD5 b91bcb0335d99ff57a00e90aa4933400
BLAKE2b-256 e4d0af9561edebdce4e88019b4892bab6c7c9778b9928a1859a3071c7a633b3d

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.5.0-cp312-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on drumtorben/polars-ts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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