Skip to main content

Convenient APIs to break lineage in complex Polars LazyFrame jobs. Can significantly reduce memory overhead for iterative processing.

Project description

polars-checkpoint

Materialise Polars LazyFrames to parquet files and scan them back lazily. Defaults to the streaming engine for sink/scan. Useful for managing & reducing memory pressure due to expensive intermediate results in complex multi-step transforms.

Installation

Requires polars and Python 3.10+.

Quick Start

import polars as pl
from polars_checkpoint import checkpoint

lf = pl.LazyFrame({"x": range(1_000_000)}).with_columns(y=pl.col("x") * 2)

# Materialises to a temp parquet file; returns a lazy re-scan
lf = checkpoint(lf)

lf.filter(pl.col("y") > 100).collect()

A process-wide default session manages the temp directory and cleans it up at exit.

Session API

For explicit control over storage location and lifecycle, use CheckpointSession:

from polars_checkpoint import CheckpointSession

# As a context manager — cleans up on exit from the block
with CheckpointSession(root_dir="./my_checkpoints") as sess:
    lf = pl.scan_csv("big.csv")
    lf = sess.checkpoint(lf, name="after-parse")
    lf = lf.filter(pl.col("status") == "active")
    lf = sess.checkpoint(lf, name="filtered")
# Without a context manager — cleans up at GC or interpreter shutdown,
# or when you call close() explicitly
sess = CheckpointSession(root_dir="./my_checkpoints")
lf = sess.checkpoint(pl.scan_csv("big.csv"), name="raw")
reloaded = sess["raw"]
print(sess.summary())

sess.close()  # optional; triggers early cleanup

CheckpointSession constructor

Parameter Default Description
root_dir None (auto temp dir) Parent directory for checkpoint folders.
cleanup True Delete checkpoint files on close / GC / interpreter exit.
default_sink_kwargs {"compression": "zstd"} Defaults passed to sink_parquet / write_parquet.
default_scan_kwargs {} Defaults passed to scan_parquet.

Key methods & features

  • checkpoint(lf, *, name=None, streaming=True, ...) — Materialise a LazyFrame to parquet. Auto-generates a name if none given. Falls back to collect().write_parquet() when streaming=False.
  • session[name] — Retrieve a checkpoint as a LazyFrame.
  • name in session — Check existence.
  • len(session) / iter(session) — Count / list checkpoints.
  • summary() — Returns a Polars DataFrame with name, size (MB), and path of each checkpoint.
  • close(timeout=None) — Waits for in-flight writes, then cleans up. Also usable as a context manager.

Thread Safety

Sessions are internally locked. Concurrent checkpoint() calls from multiple threads are safe; close() waits for all in-flight materialisations before removing files.

Cleanup Behaviour

Scenario cleanup=True (default) cleanup=False
close() / __exit__ Files deleted Files retained
GC / interpreter shutdown Files deleted (via weakref.finalize) Files retained

When root_dir is auto-generated, the entire temp directory is removed. When user-supplied, only the individual checkpoint subdirectories created by the session are removed.

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_checkpoint-0.1.3.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

polars_checkpoint-0.1.3-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file polars_checkpoint-0.1.3.tar.gz.

File metadata

  • Download URL: polars_checkpoint-0.1.3.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for polars_checkpoint-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7e3150202bbd28a65f083e2483a81ab29c302efa7fd4da885807e1c15a3e2044
MD5 cc1931a48d3ab852b57cc15482cf8748
BLAKE2b-256 ea6e3111e3733fbb349857a15e53714c50dfd1ac2ce41cddad078aeb3728aedc

See more details on using hashes here.

File details

Details for the file polars_checkpoint-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: polars_checkpoint-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for polars_checkpoint-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3c52a87f9d5cd73e6fe34a4c45e5f253955e03891be242927573f5c75a28980e
MD5 39660cb514d148233798fb7cd23eb3a3
BLAKE2b-256 1b88e1df26ead3a6f2049d85ccdc5bbd094568bd65f78cfe758cecb781318fcb

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