Skip to content

daft.functions.hamming_distance_str#

hamming_distance_str #

hamming_distance_str(left: Expression, right: Expression) -> Expression

Compute the character-level Hamming distance between two strings.

The Hamming distance is the number of positions at which the corresponding characters are different.

Parameters:

Name Type Description Default
left Expression

The left string expression to compare.

required
right Expression

The right string expression to compare against.

required

Returns:

Type Description
Expression

The Hamming distance for each pair of strings. Returns null when either input

Expression

is null or the two strings have different lengths.

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import hamming_distance_str
>>> df = daft.from_pydict({"x": ["ronald", "ronald", "ronald"], "y": ["ronald", "renuld", "ronaldo"]})
>>> df = df.with_column("distance", hamming_distance_str(df["x"], df["y"]))
>>> df.collect()
╭────────┬─────────┬──────────╮
│ x      ┆ y       ┆ distance │
│ ---    ┆ ---     ┆ ---      │
│ String ┆ String  ┆ Int64    │
╞════════╪═════════╪══════════╡
│ ronald ┆ ronald  ┆ 0        │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ ronald ┆ renuld  ┆ 2        │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ ronald ┆ ronaldo ┆ None     │
╰────────┴─────────┴──────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/str.py
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
def hamming_distance_str(left: Expression, right: Expression) -> Expression:
    """Compute the character-level Hamming distance between two strings.

    The Hamming distance is the number of positions at which the corresponding
    characters are different.

    Args:
        left: The left string expression to compare.
        right: The right string expression to compare against.

    Returns:
        The Hamming distance for each pair of strings. Returns null when either input
        is null or the two strings have different lengths.

    Examples:
        >>> import daft
        >>> from daft.functions import hamming_distance_str
        >>> df = daft.from_pydict({"x": ["ronald", "ronald", "ronald"], "y": ["ronald", "renuld", "ronaldo"]})
        >>> df = df.with_column("distance", hamming_distance_str(df["x"], df["y"]))
        >>> df.collect()
        ╭────────┬─────────┬──────────╮
        │ x      ┆ y       ┆ distance │
        │ ---    ┆ ---     ┆ ---      │
        │ String ┆ String  ┆ Int64    │
        ╞════════╪═════════╪══════════╡
        │ ronald ┆ ronald  ┆ 0        │
        ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
        │ ronald ┆ renuld  ┆ 2        │
        ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
        │ ronald ┆ ronaldo ┆ None     │
        ╰────────┴─────────┴──────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)
    """
    return Expression._call_builtin_scalar_fn("hamming_distance_str", left, right)