Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Eventual Inc Daft Expression Getitem

From Leeroopedia


Knowledge Sources
Domains Data_Engineering, Data_Transformation
Last Updated 2026-02-08 00:00 GMT

Overview

Concrete tool for accessing struct fields and list elements via bracket notation provided by the Daft library.

Description

The __getitem__ method on Daft's Expression class provides syntactic sugar for accessing nested data within struct and list columns. For string keys, it extracts the named field from a struct column. For integer keys, it extracts the element at the given position from a list column. For slice keys, it extracts a sub-list. Under the hood, this delegates to daft.functions.get (for string and int keys) and daft.functions.slice (for slice keys).

Usage

Use bracket notation on any Daft expression that references a struct or list column. This is the idiomatic way to access nested data in Daft, mirroring Python's native subscript syntax.

Code Reference

Source Location

  • Repository: Daft
  • File: daft/expressions/expressions.py
  • Lines: L385-442

Signature

def __getitem__(self, key: str | int | slice) -> Expression

Import

# Use bracket notation on expressions - no separate import needed
from daft import col

# Struct field access
col("struct_col")["field_name"]

# List element access
col("list_col")[0]

# List slice access
col("list_col")[1:3]

I/O Contract

Inputs

Name Type Required Description
key str Yes (for structs) Name of the struct field to extract
key int Yes (for lists) Positional index of the list element to extract
key slice Yes (for list slices) Slice notation specifying start and stop positions (step not supported)

Outputs

Name Type Description
return Expression A new Expression representing the extracted field, element, or sub-list

Usage Examples

Basic Usage

import daft

# Create DataFrame with struct and list columns
df = daft.from_pydict({
    "struct": [{"x": 1, "y": 2}, {"x": 3, "y": 4}],
    "list": [[10, 20], [30, 40]]
})

# Access struct field by name and list element by index
df = df.select(
    df["struct"]["x"],
    df["list"][0].alias("first")
)
df.show()
# Output:
# x: [1, 3]
# first: [10, 30]

Slice Usage

import daft

df = daft.from_pydict({"x": [[1, 2, 3], [4, 5, 6, 7], [8]]})

# Extract sub-lists using slice notation
df = df.select(df["x"][1:-1])
df.show()
# Output:
# x: [[2], [5, 6], []]

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment