Skip to content

daft.functions.concat#

concat #

concat(left: Expression | str | bytes, right: Expression | str | bytes) -> Expression

Concatenates two string or binary values.

Parameters:

Name Type Description Default
left (String or Binary Expression) | str | bytes

the left value to concatenate

required
right (String or Binary Expression) | str | bytes

the right value to concatenate

required

Returns:

Name Type Description
Expression Expression

an expression with the same type as the inputs

Examples:

String concatenation:

1
2
3
4
5
>>> import daft
>>> from daft.functions import concat
>>>
>>> df = daft.from_pydict({"x": ["foo", "bar", "baz"], "y": ["a", "b", "c"]})
>>> df.select(concat(df["x"], df["y"])).collect()
╭────────╮
│ x      │
│ ---    │
│ String │
╞════════╡
│ fooa   │
├╌╌╌╌╌╌╌╌┤
│ barb   │
├╌╌╌╌╌╌╌╌┤
│ bazc   │
╰────────╯
(Showing first 3 of 3 rows)

Binary concatenation:

1
2
3
4
5
>>> df = daft.from_pydict(
...     {"a": [b"Hello", b"\\xff\\xfe", b"", b"World"], "b": [b" World", b"\\x00", b"empty", b"!"]}
... )
>>> df = df.select(concat(df["a"], df["b"]))
>>> df.show()
╭────────────────────╮
│ a                  │
│ ---                │
│ Binary             │
╞════════════════════╡
│ b"Hello World"     │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ b"\\xff\\xfe\\x00" │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ b"empty"           │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ b"World!"          │
╰────────────────────╯
(Showing first 4 of 4 rows)
Source code in daft/functions/misc.py
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
def concat(left: Expression | str | bytes, right: Expression | str | bytes) -> Expression:
    r"""Concatenates two string or binary values.

    Args:
        left ((String or Binary Expression) | str | bytes): the left value to concatenate
        right ((String or Binary Expression) | str | bytes): the right value to concatenate

    Returns:
        Expression: an expression with the same type as the inputs

    Examples:
        String concatenation:

        >>> import daft
        >>> from daft.functions import concat
        >>>
        >>> df = daft.from_pydict({"x": ["foo", "bar", "baz"], "y": ["a", "b", "c"]})
        >>> df.select(concat(df["x"], df["y"])).collect()
        ╭────────╮
        │ x      │
        │ ---    │
        │ String │
        ╞════════╡
        │ fooa   │
        ├╌╌╌╌╌╌╌╌┤
        │ barb   │
        ├╌╌╌╌╌╌╌╌┤
        │ bazc   │
        ╰────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

        Binary concatenation:

        >>> df = daft.from_pydict(
        ...     {"a": [b"Hello", b"\\xff\\xfe", b"", b"World"], "b": [b" World", b"\\x00", b"empty", b"!"]}
        ... )
        >>> df = df.select(concat(df["a"], df["b"]))
        >>> df.show()
        ╭────────────────────╮
        │ a                  │
        │ ---                │
        │ Binary             │
        ╞════════════════════╡
        │ b"Hello World"     │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ b"\\xff\\xfe\\x00" │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ b"empty"           │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ b"World!"          │
        ╰────────────────────╯
        <BLANKLINE>
        (Showing first 4 of 4 rows)

    """
    return Expression._to_expression(left) + Expression._to_expression(right)