Skip to content

daft.functions.to_struct#

to_struct #

to_struct(*fields: Expression, **named_fields: Expression) -> Expression

Constructs a struct from the input expressions.

Parameters:

Name Type Description Default
fields Expression

Expressions to be set as struct fields, using the expression name as the field name.

()
named_fields Expression

Expressions to be set as struct fields, using the keyword arg as the field name.

{}

Returns:

Type Description
Expression

An expression for a struct column with the input columns as its fields.

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import to_struct
>>>
>>> df = daft.from_pydict({"a": ["a", "b", "c"], "b": [1, 2, 3]})
>>> df.select(to_struct(df["a"], b2=df["b"] * 2)).show()
╭──────────────────────────────╮
│ struct                       │
│ ---                          │
│ Struct[a: String, b2: Int64] │
╞══════════════════════════════╡
│ {a: a,                       │
│ b2: 2,                       │
│ }                            │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ {a: b,                       │
│ b2: 4,                       │
│ }                            │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ {a: c,                       │
│ b2: 6,                       │
│ }                            │
╰──────────────────────────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/struct.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def to_struct(*fields: Expression, **named_fields: Expression) -> Expression:
    """Constructs a struct from the input expressions.

    Args:
        fields: Expressions to be set as struct fields, using the expression name as the field name.
        named_fields: Expressions to be set as struct fields, using the keyword arg as the field name.

    Returns:
        An expression for a struct column with the input columns as its fields.

    Examples:
        >>> import daft
        >>> from daft.functions import to_struct
        >>>
        >>> df = daft.from_pydict({"a": ["a", "b", "c"], "b": [1, 2, 3]})
        >>> df.select(to_struct(df["a"], b2=df["b"] * 2)).show()
        ╭──────────────────────────────╮
        │ struct                       │
        │ ---                          │
        │ Struct[a: String, b2: Int64] │
        ╞══════════════════════════════╡
        │ {a: a,                       │
        │ b2: 2,                       │
        │ }                            │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ {a: b,                       │
        │ b2: 4,                       │
        │ }                            │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ {a: c,                       │
        │ b2: 6,                       │
        │ }                            │
        ╰──────────────────────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)
    """
    all_fields = list(fields) + [Expression._to_expression(field).alias(name) for name, field in named_fields.items()]
    return Expression._call_builtin_scalar_fn("struct", *all_fields)