Skip to main content

A lightweight caching library for polars

Project description

polars_cache

A lightweight, lazy, disc-based cache for Polars LazyFrames.

Usage

import polars as pl
import polars_cache as pc

lf = pl.LazyFrame({"x" : range(100)})

def very_expensive(col: str):
    pl.col(col).pow(2).exp().sqrt()

query = (
    lf
    .with_columns(very_expensive("x"))
    .pipe(pc.cache_to_disc, max_age=120) # set up cache
)

df1 = query.collect()  # populate the cache
df2 = query.collect()  # second invocation will be much faster!

# do some downstream computation
another_query = query.with_columns(y = pl.col("x") + 7)

df3 = another_query.collect() # this will use the cache!

Updating a source will cause the cache to refresh:

import os

query_from_a_file = (
    pl.scan_parquet("data.parquet")
    .group_by("age", "sex")
    .agg(pl.len())
    .pipe(pc.cache_to_disc, check_sources=True)
)

_ = query_from_a_file.collect() # populate cache
result = query_from_a_file.collect() # load from cache

os.utime("data.parquet")  # update source timestamp
new_result = query_from_a_file.collect() # cache is invalid -- will refresh

⚠️ Warning ⚠️

This function is opaque to the Polars optimizer and will split your query into two chunks: one before the cache statment and one after. Each query will be independently optimzed by Polars, but optimizations (e.g. projection and predicate pushdown) will NOT be able to cross the cache barrier. Use with caution.

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_cache-1.0.5.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

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

polars_cache-1.0.5-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file polars_cache-1.0.5.tar.gz.

File metadata

  • Download URL: polars_cache-1.0.5.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.3

File hashes

Hashes for polars_cache-1.0.5.tar.gz
Algorithm Hash digest
SHA256 5999acd98a04f6a392aae28cb1085cc3aa1e22156ca640ad74bbec0c655c1e0e
MD5 96ad334fe0f1e9da5b43598d5767190b
BLAKE2b-256 cab46f791898872e2a10f9cac17e0b2a0fdf98449c79fe09745c1f1bcb142de4

See more details on using hashes here.

File details

Details for the file polars_cache-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for polars_cache-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1b7b530eaaf918d7689864a8c89d85f126286f36396e2cb34228a6bf8da7195f
MD5 1e8a92c05b6dffcdf7e0ee5588ee63f0
BLAKE2b-256 0385ee6e874719ee1198d5a8d1323a149335f2cd4498a2b39c858e944370025a

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