Lazy DataFrame lifecycle
Daft's Python DataFrame is a table workflow that lets users describe data work before they run it. Construction creates a source and schema view. Transformations extend that workflow. A materialization, display, iteration, or export call then runs the work and returns rows, partitions, or data for another Python system. See the parent page DataFrame API and lazy workflows, with related context in Expressions and functions and Data model and micropartitions.
Lifecycle model
The lifecycle separates describing a table from producing its values. This separation lets a workflow combine reads and transformations before the execution system chooses how to run the complete plan.
flowchart LR
A[Input data or source] --> B[DataFrame with schema]
B --> C[Lazy transformations]
C --> D[Schema inspection]
C --> E[Materialization]
E --> F[Collected DataFrame]
E --> G[Streaming rows or partitions]
E --> H[External format or system]
F --> I[Display or conversion]
DataFrame identity
The DataFrame represents the current table workflow rather than a Python list of rows. Each transformation returns a new DataFrame, so a prior value can be reused as the input to more than one downstream workflow. The workflow keeps a schema view and can keep materialized results for later inspection or reuse.
References: daft/…/dataframe.py• L181-237
Construction paths
Public construction functions turn common Python and external table forms into a DataFrame. In-memory values come from row dictionaries, column dictionaries, Arrow tables or streams, and pandas tables. Source functions create DataFrames for files, tables, streams, generators, and other data systems. These sources can remain unmaterialized until a later call.
The main construction choices are:
| Input shape | Public entry point | Resulting use |
|---|---|---|
| Rows keyed by column name | daft.from_pylist | Small or already assembled Python data |
| Columns keyed by name | daft.from_pydict | Python, NumPy, or Arrow column values |
| Arrow-compatible data | daft.from_arrow | Arrow tables, batches, and stream producers |
| pandas data | daft.from_pandas | Data exchanged with pandas |
| Ray or Dask data | daft.from_ray_dataset, daft.from_dask_dataframe | Ray-backed distributed workflows |
| Files, tables, or streams | daft.read_* and daft.from_* source functions | Data that Daft reads during execution |
In-memory inputs have an initial result available, while source inputs usually describe work that must be performed later. Both forms expose the same DataFrame lifecycle to the caller.
References: daft/convert.py• L30-172 daft/convert.py• L175-312 daft/…/__init__.py• L21-40 daft/…/dataframe.py• L805-904
Transformation chaining
Transformations describe the desired table result and return another DataFrame. They accept column names or expressions, which allows column selection, derived values, and row rules to remain part of one chain instead of forcing Python-side row processing.
Column and row shaping
The core shaping operations change which columns and rows belong to the result. select chooses or derives columns. with_column and with_columns add derived columns. Rename and exclusion operations change the visible column set. filter and where keep rows that satisfy a predicate. limit and offset narrow the row range. Other shaping operations can remove missing values, explode list values into rows, reshape wide tables, sample rows, or remove duplicates.
These calls do not mutate the source DataFrame. A typical chain can therefore keep a raw input for reuse while building a focused result:
result = (
raw
.with_column("total", raw["price"] * raw["quantity"])
.where(raw["total"] > 0)
.select("item", "total")
)
The expression system resolves column references against the current schema. A later operation sees the schema produced by the prior operation. Indexing a single column returns an expression. Indexing several columns or a column slice returns a new DataFrame.
References: daft/…/dataframe.py• L2748-2845 daft/…/dataframe.py• L2886-2919 daft/…/dataframe.py• L3165-3282 daft/…/dataframe.py• L3455-3593 daft/…/dataframe.py• L3692-3764 daft/…/dataframe.py• L4178-4500
Table combination
DataFrames can combine rows or relate columns from separate workflows. Concatenation requires matching schemas. Union and intersection operations express set-style combinations, with separate forms for preserving duplicates or matching columns by name. Joins relate two DataFrames through key expressions and produce a combined schema. An as-of join matches each left row to a nearby right row by an ordered key. Grouping creates a grouped view whose aggregation returns a new DataFrame.
The important distinction is that these methods combine table descriptions first. Their row matching, duplicate handling, and grouped results occur when the resulting workflow is materialized.
References: daft/…/dataframe.py• L3920-4130 daft/…/dataframe.py• L4132-4176 daft/…/dataframe.py• L5142-5250 daft/…/dataframe.py• L5252-5520
Partition shape
Partition operations describe how work is divided for execution. repartition can redistribute rows. into_partitions can split or combine existing partitions while preserving order. into_batches targets a row count per output batch. These operations affect execution shape rather than the table's columns and values.
Global redistribution and global ordering can be costly. Use a partition operation when the downstream consumer needs a particular work shape, not only because the DataFrame has a desired display size.
References: daft/…/dataframe.py• L3595-3690 daft/…/dataframe.py• L3810-3918
Function composition
transform and pipe let a Python function package a group of DataFrame operations while keeping the outer chain readable. The function in transform must return a DataFrame. pipe is more general and returns whatever the supplied function returns, so it can also mark a hand-off to another API.
This boundary is useful for naming business steps without changing the lazy model. The function should describe DataFrame work. Execution still belongs to a later materialization or export call.
References: daft/…/dataframe.py• L273-312 daft/…/dataframe.py• L4556-4599
Schema-facing behavior
The schema is the DataFrame's column contract. It names each field and gives its Daft data type. Daft exposes this contract while the workflow is still lazy, so code can select columns, build expressions, check compatibility, and prepare downstream metadata before it reads all rows.
Schema inspection
df.schema() returns a Schema object. df.column_names returns names in column order. df.columns returns column expressions that refer to the current columns. Membership checks such as "name" in df also inspect the schema and do not require row materialization.
Schema inspection is not the same as asking for values. Operations such as len(df) are different: len requires a materialized result, while count_rows() runs a count workflow when only the row count is needed.
References: daft/…/dataframe.py• L464-504 daft/…/dataframe.py• L5708-5750
Schema evolution
Transformations can preserve, remove, rename, or add fields. Expressions can also change field types. A concatenation requires exact schema equality. A join adds fields from both inputs and resolves name collisions with a right-side name unless the caller supplies another prefix or suffix. Set operations can match columns by position or by name. These rules make schema inspection the first check when two workflows must meet.
The schema is also the bridge to external systems. Arrow conversion uses it to build the output table schema. Dask conversion can use it as default metadata. Source readers can infer or accept a schema that defines the fields available to later expressions.
References: daft/…/dataframe.py• L3920-4130 daft/…/dataframe.py• L4132-4176 daft/…/dataframe.py• L5252-5520 daft/…/dataframe.py• L5752-5833 daft/…/dataframe.py• L6145-6195 daft/…/_parquet.py• L24-59
Materialization entry points
Materialization turns the lazy workflow into data. The runner executes the current plan and can retain the result on the DataFrame for later inspection or export. Calls that need values may execute the full workflow or stream its output, so the choice of entry point controls both when work starts and how much data the caller holds.
Collection
collect() is the direct, blocking entry point. It executes the entire DataFrame and returns the same DataFrame with its result available. A later collect() reuses the stored result. Execution metadata becomes available with the materialized result, including details about skipped corrupt files when the source was configured to ignore them.
Use collect() when the caller needs a reusable, fully materialized DataFrame. Use count_rows() when only the row count is needed. len(df) is for an already collected DataFrame and raises for an unmaterialized one.
References: daft/…/dataframe.py• L5522-5579 daft/…/dataframe.py• L3785-3808 daft/…/dataframe.py• L5708-5732
Streaming iteration
Iteration starts execution without requiring the caller to first collect the full result. iter_rows() yields one dictionary per row. Its default Python form is convenient for normal values. The Arrow form keeps nested values as Arrow scalars. to_arrow_iter() yields Arrow record batches. iter_partitions() yields Daft micropartitions or Ray object references according to the active runner.
Each streaming API has a result buffer. A larger buffer can improve throughput but uses more memory. A smaller buffer limits in-flight work. This makes these APIs suitable for consumers that can process data incrementally.
References: daft/…/dataframe.py• L506-742
Plan inspection
explain() inspects the workflow before or around execution. By default it shows the unoptimized logical plan. With show_all=True, it also shows the optimized logical plan and the physical plan; that option runs the optimizer. ASCII output is printed, while Mermaid output supports notebook display or a returned representation.
Plan inspection answers a different question from collection: it shows how Daft intends to produce the result rather than returning the result rows.
References: daft/…/dataframe.py• L314-423 daft/…/display.py• L49-108
Display behavior
Display is a preview interface, not a replacement for collection. show() executes enough of the workflow to display the first n rows. It stops when it has enough rows or when it knows that the input has fewer rows. The preview includes column names and can include types, and it reports whether the result is partial or complete.
Explicit previews
show(n=...) blocks until it has enough rows for the preview or knows that the input has fewer rows. It supports text formats such as fancy, plain, simple, grid, and Markdown, plus width and alignment controls. Per-column settings override global settings. In a notebook, Daft uses rich display when available and can fall back to a plain representation.
The display configuration can come from the call or from DAFT_SHOW_FORMAT, DAFT_SHOW_VERBOSE, DAFT_SHOW_MAX_WIDTH, and DAFT_SHOW_ALIGN. A preview from show() runs enough of an unmaterialized workflow to produce its bounded result. It does not imply that the whole workflow has run.
References: daft/…/dataframe.py• L5581-5706 daft/…/preview.py• L19-130 daft/…/preview.py• L180-244
Implicit representations
Python representation hooks provide text and HTML views for notebooks and interactive shells. They use cached preview data when it exists. If the DataFrame has no cached result, they show schema information without forcing a complete collection. This keeps inspection useful while preserving the lazy boundary.
Interactive notebook display can use a dashboard-backed HTML view when the required display services are available. If that path fails, the formatter falls back to ordinary HTML or text output.
References: daft/…/dataframe.py• L743-799 daft/…/preview.py• L198-264
Conversion APIs
Conversion APIs define how a Daft result leaves the DataFrame interface. Full-table conversions are blocking and produce one in-memory result. Streaming conversions preserve incremental execution and are better when the next system can consume batches or rows as they arrive.
Python and Arrow conversions
The main full-result forms are:
| API | Output | Materialization behavior |
|---|---|---|
to_pydict() | Dictionary of column names to Python lists | Blocks and collects |
to_pylist() | List of row dictionaries | Blocks and collects; holds all rows in a Python list |
to_arrow() | One PyArrow table | Blocks and builds a table from Arrow record batches |
to_pandas() | pandas DataFrame | Blocks and collects the result |
__arrow_c_stream__ | Arrow C stream interface | Blocks and exposes the collected result to Arrow consumers |
to_pylist() is convenient for small results. iter_rows() or to_arrow_iter() is a better boundary for a stream because it avoids building the full Python object in one step. Map columns need an explicit policy when converting to Python dictionaries: the default preserves key order and duplicates as association lists, while lossy or strict dictionary modes handle duplicate keys differently.
References: daft/…/dataframe.py• L5752-5900
Machine learning conversions
The DataFrame can feed PyTorch through a map-style dataset, an iterable dataset, or a Daft data loader that yields batches. The map-style form materializes the full result. The iterable forms start execution as the consumer reads data. They can use file-based sharding when the caller supplies worker rank information.
Ray conversion produces a Ray Dataset after collection and works with either the native or Ray runner when Ray is installed. Dask conversion requires the Ray runner and returns a Dask DataFrame backed by Dask-on-Ray. These APIs are hand-off points to other execution or training systems, not alternate lazy views of the same Daft plan.
References: daft/…/dataframe.py• L5902-6099 daft/…/dataframe.py• L6145-6195
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the public constructors aligned with the input types that
DataFramecan turn into a source or in-memory result. - Keep schema propagation aligned between transformation methods,
Schema, and external conversion metadata. - Keep blocking entry points clear about whether they collect a full result or only compute a count or preview.
- Keep streaming result-buffer behavior aligned across row, Arrow-batch, partition, and PyTorch consumers.
- Keep Python map conversion rules aligned between
to_pydict(),to_pylist(),RecordBatch, andMicroPartition. - Keep Ray and Dask conversion constraints aligned with the active runner and with the corresponding reverse conversion APIs.
- Keep set-operation choices aligned with their duplicate and column-matching rules.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
Calling len(df) before collection | Raises a runtime error instead of counting rows | Call collect() first or use count_rows() |
| Concatenating DataFrames with unequal schemas | Raises a schema mismatch error | Inspect schema() and align names and types before concatenation |
Using to_pylist() for a large result | Holds every row in Python memory | Use iter_rows() for row streaming |
| Passing an invalid or non-positive streaming buffer size to Arrow-batch or partition iteration | Raises a value error or creates an invalid stream setting | Use None or a positive buffer size |
| Converting to Dask without the Ray runner | Conversion fails because Dask-on-Ray needs Ray-backed partitions | Use the Ray runner for Dask workflows |
Expecting show() to collect all rows | Only a bounded preview is produced | Use collect() or a full conversion when all rows are required |
| Using a join key or set-operation input that does not match the required schema | Plan construction or execution fails, or columns receive unexpected nulls or renamed conflict fields | Check key names, types, and the chosen positional or name-based operation before materialization |
Key files
| File | Why to read it |
|---|---|
| daft/…/dataframe.py | Defines the public DataFrame lifecycle from construction and schema access through chaining, execution, display, and conversion. |
| daft/convert.py | Defines the top-level in-memory and external DataFrame constructors and concatenation entry point. |
| daft/…/__init__.py | Shows the public family of source functions that enter the same DataFrame lifecycle. |
| daft/…/builder.py | Shows the boundary where DataFrame actions become a logical workflow with a schema. |
| daft/…/preview.py | Defines preview state, display defaults, and user-facing messages for materialized and unmaterialized data. |
| daft/…/display.py | Defines plan display formats used by explain(). |
| daft/…/micropartition.py | Shows the tabular result boundary used for partitions and Arrow, Python, and pandas conversion. |
| daft/schema.py | Defines the Python schema and field contract exposed during the lazy lifecycle. |