Skip to main content

DataFrame utilities for Chalk AI, backed by libchalk

Project description

chalkdf

DataFrame utilities for building fast, portable data pipelines on top of Apache Arrow — powered by Chalk’s libchalk execution engine.

The API centers on two concepts:

  • chalkdf.DataFrame: a lightweight, eager plan that you can materialize to Arrow.
  • chalkdf.LazyFrame: a serializable, chainable description of a plan that can be round-tripped to/from a protobuf and converted to a DataFrame for execution.

You build expressions with Chalk’s Python underscore DSL (from chalk import _) and function registry (import chalk.functions as F). We also ship pragmatic testing helpers (chalkdf.Testing) to make it easy to compare results in your unit tests.

Installation

  • Requires Python 3.10–3.13
  • Works on Linux and macOS 15+

Install from PyPI:

pip install chalkdf chalkpy

Quickstart

Below are minimal, self‑contained examples that mirror the public API used in the tests.

Create from Arrow and select columns

import pyarrow as pa
from chalkdf import DataFrame

tbl = pa.table({"a": [1, 2, 3], "b": [10, 20, 30], "c": [100, 200, 300]})
df = DataFrame.from_arrow(tbl)

out = df.select("c", "a")
print(out.run())
# ┌─────┬─────┐
# │  c  │  a  │
# ├─────┼─────┤
# │ 100 │  1  │
# │ 200 │  2  │
# │ 300 │  3  │
# └─────┴─────┘

Add or replace columns with expressions

import pyarrow as pa
import chalk.functions as F
from chalk import _
from chalkdf import DataFrame

tbl = pa.table({"a": pa.array([1, 2, 3], pa.int64()), "b": pa.array([10, 20, 30], pa.int64())})
df = DataFrame.from_arrow(tbl)

out = df.with_columns(
    {
        "sum": _.a + _.b,
        "flag": F.if_then_else(_.a < 2, "small", "big"),
    }
)
print(out.run())

Filter, slice, and order

import pyarrow as pa
from chalk import _
from chalkdf import DataFrame

tbl = pa.table({"a": pa.array([1, 2, 3, 4, 5], pa.int64()), "b": pa.array([10, 20, 30, 40, 50], pa.int64())})
df = DataFrame.from_arrow(tbl)

out = df.filter(_.a > 2).slice(1, 2).order_by(("b", "descending"))
print(out.run())

Force caching of a reused subcomputation

DataFrame.replay() is an escape hatch for tests and carefully inspected plans where you need to force a subcomputation to be cached behind an explicit replay boundary. Most plans should let the optimizer place replay nodes automatically; use this only when you know a shared subplan is expensive enough to compute once and reuse.

from chalk import _
from chalkdf import DataFrame

df = DataFrame.from_dict({"id": [1, 2, 3], "amount": [10, 20, 30]})
shared = df.with_columns({"amount_cents": _.amount * 100}).replay("shared_amount_cents")

Rename and explode

import pyarrow as pa
from chalkdf import DataFrame

tbl = pa.table({"id": [1, 2], "vals": pa.array([[1, 2], []], type=pa.list_(pa.int64()))})
df = DataFrame.from_arrow(tbl)

renamed = df.rename({"vals": "values"})
exploded = renamed.explode("values")
print(exploded.run())  # (1,1), (1,2)

Group by and aggregate

import pyarrow as pa
from chalk import _
from chalkdf import DataFrame

tbl = pa.table({"g": [1, 1, 2, 2, 2], "v": pa.array([10, 20, 1, 2, 3], pa.int64())})
df = DataFrame.from_arrow(tbl)

out = df.agg(["g"], _.v.sum().alias("v_sum"))
print(out.run())  # rows may be unordered

Joins

Use a list of join keys when both sides share column names. When names differ, pass a mapping of left columns to their right-hand counterparts.

import pyarrow as pa
from chalkdf import DataFrame

left = DataFrame.from_arrow(pa.table({"key": [1, 2, 3], "x": [10, 20, 30]}))
right = DataFrame.from_arrow(pa.table({"key": [2, 3, 4], "y": [200, 300, 400]}))

joined = left.join(right, on=["key"], how="inner").select("key", "x", "y")
print(joined.run())

# Or with an explicit mapping of left->right keys
joined2 = left.join(right, on={"key": "key"}, how="inner")

# When column names differ, map each left column to its right counterpart
joined3 = left.join(right, on={"key": "lookup_key"}, how="left")

Scan Parquet files

You can construct a DataFrame that scans one or more Parquet files without loading them eagerly. Use local file:// URIs (or remote URIs when running in an environment with appropriate access):

from chalkdf import DataFrame

df = DataFrame.scan(
    ["file:///path/to/data.parquet"],
    name="my_table",
)

print(df.run())  # materializes and prints a preview

Lazy plans (experimental)

LazyFrame records a chain of operations and can round‑trip to a protobuf for transport or persistence. You can reconstruct the same lazy plan later and convert it to a DataFrame to execute.

from chalk import _
from chalkdf import LazyFrame

lf = (
    LazyFrame.from_dict({"c1": [1, 2, 3], "c2": [4, 5, 6]})
    .select("c1", "c2")
    .slice(0, length=10)
    .filter(_.c1 > 0)
)

proto = lf.to_proto()
lf2 = LazyFrame.from_proto(proto)
assert lf == lf2  # structural equality of the recorded plan

df = lf2._convert_to_df()  # convert to a DataFrame for execution
print(df.run())

Notes:

  • LazyFrame._convert_to_df() is currently a private/experimental helper.
  • For named tables, use LazyFrame.named_table("table_name", schema) as a root.

Testing helpers

Use chalkdf.Testing.assert_frame_equal to compare DataFrame results in unit tests. It materializes both frames to Arrow and supports relaxed comparisons.

import pyarrow as pa
from chalkdf import Testing, DataFrame

left = DataFrame.from_arrow(pa.table({"a": [1.000001], "b": [2.0]}))
right = DataFrame.from_arrow(pa.table({"a": [1.0], "b": [2.0]}))

Testing.assert_frame_equal(left, right, atol=1e-5, rtol=0.0)

# Ignore row or column order when needed
Testing.assert_frame_equal(left, right, check_row_order=False, check_column_order=False)

Why chalkdf?

  • Built on Apache Arrow memory formats for zero‑copy interop.
  • Runs on Chalk’s native engine for performance and portability.
  • Small, predictable API surface suited for services and batch jobs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

chalkdf-3.38.4-cp313-cp313-manylinux_2_36_x86_64.whl (177.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.36+ x86-64

chalkdf-3.38.4-cp313-cp313-macosx_15_0_arm64.whl (142.7 MB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

chalkdf-3.38.4-cp312-cp312-manylinux_2_36_x86_64.whl (177.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.36+ x86-64

chalkdf-3.38.4-cp312-cp312-macosx_15_0_arm64.whl (142.7 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

chalkdf-3.38.4-cp311-cp311-manylinux_2_36_x86_64.whl (177.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.36+ x86-64

chalkdf-3.38.4-cp311-cp311-macosx_15_0_arm64.whl (142.7 MB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

chalkdf-3.38.4-cp310-cp310-manylinux_2_36_x86_64.whl (177.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.36+ x86-64

chalkdf-3.38.4-cp310-cp310-macosx_15_0_arm64.whl (142.7 MB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file chalkdf-3.38.4-cp313-cp313-manylinux_2_36_x86_64.whl.

File metadata

File hashes

Hashes for chalkdf-3.38.4-cp313-cp313-manylinux_2_36_x86_64.whl
Algorithm Hash digest
SHA256 89349c3fdf16d6231e0d1b4dd60b8e3ad5a62e804b40bd32486b18af473ac6b2
MD5 cb011cec35175f2d164d7a79ff7f5223
BLAKE2b-256 c968f055cbcc4b90e9c18c7c0bbc0cdde6cc1e26b8be7ed4fca22629bb5f0215

See more details on using hashes here.

Provenance

The following attestation bundles were made for chalkdf-3.38.4-cp313-cp313-manylinux_2_36_x86_64.whl:

Publisher: build-dataframe.yml on chalk-ai/chalk-private

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

File details

Details for the file chalkdf-3.38.4-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for chalkdf-3.38.4-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 fbe276c039930b9b3c3d9396c84a6f2f2d9670245a237359815e3e99ca331813
MD5 b246c9675eba60c0d629cdee477c84b1
BLAKE2b-256 036cb28aafe733defcd3174eb241c4502dab8893807f4012b22177179071b12e

See more details on using hashes here.

Provenance

The following attestation bundles were made for chalkdf-3.38.4-cp313-cp313-macosx_15_0_arm64.whl:

Publisher: build-dataframe.yml on chalk-ai/chalk-private

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

File details

Details for the file chalkdf-3.38.4-cp312-cp312-manylinux_2_36_x86_64.whl.

File metadata

File hashes

Hashes for chalkdf-3.38.4-cp312-cp312-manylinux_2_36_x86_64.whl
Algorithm Hash digest
SHA256 85e259ee6b079044fd145cda5dbf800d925fe6c32880610647a44b633f6645f0
MD5 f2ad4bf3d0f772bbd601668d4e8ed0ae
BLAKE2b-256 0903cf71da2e5ce4d169de95b6eaa256f98fb7c0a8daa9d348ec7ebdee88772d

See more details on using hashes here.

Provenance

The following attestation bundles were made for chalkdf-3.38.4-cp312-cp312-manylinux_2_36_x86_64.whl:

Publisher: build-dataframe.yml on chalk-ai/chalk-private

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

File details

Details for the file chalkdf-3.38.4-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for chalkdf-3.38.4-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 65f9c61d281a18ee5170d522c50ffcaebd9a455c01a4b27ede6f8011f2cb5d0f
MD5 c4009734a646951aacd34354db36e3ac
BLAKE2b-256 019b9882d5b6bbec8f45069c555425edc275a3903a6574d5a245dcf2a95b9525

See more details on using hashes here.

Provenance

The following attestation bundles were made for chalkdf-3.38.4-cp312-cp312-macosx_15_0_arm64.whl:

Publisher: build-dataframe.yml on chalk-ai/chalk-private

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

File details

Details for the file chalkdf-3.38.4-cp311-cp311-manylinux_2_36_x86_64.whl.

File metadata

File hashes

Hashes for chalkdf-3.38.4-cp311-cp311-manylinux_2_36_x86_64.whl
Algorithm Hash digest
SHA256 2ddf099f54448cd0c4b03e64aae88f1e0c1233172e8bacdc8e023f78e4ea1710
MD5 55326822cb6aafbc01c757cb46fab565
BLAKE2b-256 95c051b056601a46753090c0dfa9c9c7408d3e98a460db80e5ae4ddd0e4cdead

See more details on using hashes here.

Provenance

The following attestation bundles were made for chalkdf-3.38.4-cp311-cp311-manylinux_2_36_x86_64.whl:

Publisher: build-dataframe.yml on chalk-ai/chalk-private

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

File details

Details for the file chalkdf-3.38.4-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for chalkdf-3.38.4-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 aea8ec6f90a9d5e0b0cb2b2453563cad43fdc1cc5426e5566306c32bac418900
MD5 bac67165f9468d3bb1e277dafe72e478
BLAKE2b-256 4213efc3332b637080df755e3dd4ca54d7cdd53f6c8d3e82f161807420bb8709

See more details on using hashes here.

Provenance

The following attestation bundles were made for chalkdf-3.38.4-cp311-cp311-macosx_15_0_arm64.whl:

Publisher: build-dataframe.yml on chalk-ai/chalk-private

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

File details

Details for the file chalkdf-3.38.4-cp310-cp310-manylinux_2_36_x86_64.whl.

File metadata

File hashes

Hashes for chalkdf-3.38.4-cp310-cp310-manylinux_2_36_x86_64.whl
Algorithm Hash digest
SHA256 b009666d8e7cbb45b0a4691a222675887b6f5ee73627fabbabb4bcaef71c01ef
MD5 ed149720333222b1766b13b162ef5033
BLAKE2b-256 4d1192f97e0e594e4bf5b9e75279ecb30dd3cc54e68ab19b11b1ff2ba651c427

See more details on using hashes here.

Provenance

The following attestation bundles were made for chalkdf-3.38.4-cp310-cp310-manylinux_2_36_x86_64.whl:

Publisher: build-dataframe.yml on chalk-ai/chalk-private

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

File details

Details for the file chalkdf-3.38.4-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for chalkdf-3.38.4-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 b37290a6afa0dd722f00291de9adb0a151212961941676c5e3bc62b3d691a0c6
MD5 af5cf78387d4ad94d13dfc317185d0e4
BLAKE2b-256 08aca7a9c8661ce6ef14278c934054a04214ac2c133c6caeb722cef6bc63501d

See more details on using hashes here.

Provenance

The following attestation bundles were made for chalkdf-3.38.4-cp310-cp310-macosx_15_0_arm64.whl:

Publisher: build-dataframe.yml on chalk-ai/chalk-private

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