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

Classification & Clustering (Python, built on distance metrics)

Algorithm Function Key Parameters
k-Nearest Neighbors knn_classify k, method (any distance metric)
k-Medoids (PAM) kmedoids k, method (any distance metric), seed

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({
    "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)],
})

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.4.0.tar.gz (306.2 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.4.0-cp312-abi3-win_amd64.whl (5.4 MB view details)

Uploaded CPython 3.12+Windows x86-64

polars_timeseries-0.4.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (6.1 MB view details)

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

polars_timeseries-0.4.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (5.3 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

polars_timeseries-0.4.0-cp312-abi3-macosx_11_0_arm64.whl (4.9 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

polars_timeseries-0.4.0-cp312-abi3-macosx_10_12_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: polars_timeseries-0.4.0.tar.gz
  • Upload date:
  • Size: 306.2 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.4.0.tar.gz
Algorithm Hash digest
SHA256 832fb9e9e7f5b9ab64139b637446accda7375332338685fad66cad4a17e53fc8
MD5 6c041cd4fca595b0f6d1a5c117386c49
BLAKE2b-256 8d6874c278d9951269f219c5db5daa33ff5797c14e9a99f8c70c638612e9654f

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.4.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.4.0-cp312-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.4.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 76f9d2cdc870c626e07b13d4750234381a5a15f18960faa69052196162d4152f
MD5 c1611ae42156c4a8e6784131db10b110
BLAKE2b-256 4de74a0a6b7cffb5a07fc1233eefd9055900561c213b5fd77484db287ffb6abf

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.4.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.4.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.4.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7f66fd4921f5d3ef9e2f498f41d5750345553faac2ddc383edc89b817f62f52c
MD5 dca16b0dd31163bfc78712a22edd5391
BLAKE2b-256 72627b579813bdbcc6628a26ceb8aec7624547021629fa52ff7d139910030751

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.4.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.4.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.4.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 daa81f879c098a12a939c7dfaa8c38353af9a2d03d977fc82b0f90b771aff4b7
MD5 719fe2ac8acada56bf1c0fe3b1330c53
BLAKE2b-256 8426de7d40bc8df2ac91c84e50e7c69fd5c2e4ab231e6ea14a7a622c24508d85

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.4.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.4.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.4.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b8b16cd1c3c1b12cc9a611f8778e94e575a9c8be5798725edd18b8540c9c0972
MD5 1e73bb16995891966a06604c25deebd9
BLAKE2b-256 da25463e506679a54b1a94cd3b0fcbfcd903fc6e3c131f5f2e398d58434a7b31

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.4.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.4.0-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for polars_timeseries-0.4.0-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 899d8977e7829aedf06e4060d968eee91932d702727ced4181447f390aaacaf6
MD5 ece2e9177b9f3eb5067894ae1224e512
BLAKE2b-256 4f35d7694f654d31d45ec549fdc633877c57a385dc092d9c818501a4b6d7517e

See more details on using hashes here.

Provenance

The following attestation bundles were made for polars_timeseries-0.4.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