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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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)