Skip to main content

Polars-based splitter functionalities for polars LazyFrames and DataFrames, similar to `sklearn.model_selection.train_test_split` and `sklearn.model_selection.StratifiedKFold`.

Project description

polars-splitters

stratified splitting of polars dataframes

Tests Package version Supported Python versions

Polars-based splitter functionalities for polars DataFrames similar to sklearn.model_selection.train_test_split and sklearn.model_selection.StratifiedKFold.

features

  • split_into_train_eval
  • split_into_k_folds
  • sample: stratified sampling

installation

pip install polars-splitters

usage

import polars as pl
from polars_splitters import split_into_train_eval, split_into_k_folds, sample

df = pl.DataFrame(
    {
        "feature_1": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
        "treatment": [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
        "outcome": [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
    }
)

# Split into train and eval
df_train, df_test = split_into_train_eval(
    df,
    eval_rel_size=0.4,
    stratify_by=["treatment", "outcome"],
    shuffle=False,
)
print(df_train, df_test)

# Split into k folds
folds = split_into_k_folds(
    df,
    k=3,
    stratify_by=["treatment", "outcome"],
    shuffle=False,
)

# e.g. get the pair df_train, df_eval for the first fold
df_train, df_val = folds[0]["train"], folds[0]["eval"]
print(df_train, df_val)

# Stratified sample
df_sample = sample(
    df,
    fraction=0.5,
    stratify_by=["treatment", "outcome"],
)

print(df_sample)

current limitations

  • only supports polars eager API (pl.DataFrame): no pl.LazyFrame

future work

  • improve resilience against ties in quantized float columns in stratify_by

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_splitters-0.4.5.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

polars_splitters-0.4.5-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file polars_splitters-0.4.5.tar.gz.

File metadata

  • Download URL: polars_splitters-0.4.5.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.24

File hashes

Hashes for polars_splitters-0.4.5.tar.gz
Algorithm Hash digest
SHA256 88bd6c631a0be05b5859aebd13cca2c23447eb0fd798506c6c12557bc7b8f412
MD5 489a0bc8ed2cd0fa0bf5cf2579df8b56
BLAKE2b-256 8ece0e21a2194f4ee8a0dc74cdf32896016ba7e5088423f8d5a6370a52c41676

See more details on using hashes here.

File details

Details for the file polars_splitters-0.4.5-py3-none-any.whl.

File metadata

File hashes

Hashes for polars_splitters-0.4.5-py3-none-any.whl
Algorithm Hash digest
SHA256 498486057df1ddc2cbe0b740fda821b5e109af63cf56fbc24d59660418a928dd
MD5 c60163ea576c21570894659b960e0100
BLAKE2b-256 9438b5aad289c0d15863c769484e47991783d365016acbc7c70d2451db8cb519

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