Skip to content

daft.functions.map_get#

map_get #

map_get(expr: Expression, key: Expression) -> Expression

Retrieves the value for a key in a map column.

Parameters:

Name Type Description Default
expr Expression

the map expression to get from

required
key Expression

the key to retrieve

required

Returns:

Name Type Description
Expression Expression

the value expression

Examples:

1
2
3
4
5
6
>>> import pyarrow as pa
>>> import daft
>>> pa_array = pa.array([[("a", 1)], [], [("b", 2)]], type=pa.map_(pa.string(), pa.int64()))
>>> df = daft.from_arrow(pa.table({"map_col": pa_array}))
>>> df = df.with_column("a", df["map_col"].map_get("a"))
>>> df.show()
╭────────────────────┬───────╮
│ map_col            ┆ a     │
│ ---                ┆ ---   │
│ Map[String: Int64] ┆ Int64 │
╞════════════════════╪═══════╡
│ {"a": 1}           ┆ 1     │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ {}                 ┆ None  │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ {"b": 2}           ┆ None  │
╰────────────────────┴───────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/misc.py
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
def map_get(expr: Expression, key: Expression) -> Expression:
    """Retrieves the value for a key in a map column.

    Args:
        expr: the map expression to get from
        key: the key to retrieve

    Returns:
        Expression: the value expression

    Examples:
        >>> import pyarrow as pa
        >>> import daft
        >>> pa_array = pa.array([[("a", 1)], [], [("b", 2)]], type=pa.map_(pa.string(), pa.int64()))
        >>> df = daft.from_arrow(pa.table({"map_col": pa_array}))
        >>> df = df.with_column("a", df["map_col"].map_get("a"))
        >>> df.show()
        ╭────────────────────┬───────╮
        │ map_col            ┆ a     │
        │ ---                ┆ ---   │
        │ Map[String: Int64] ┆ Int64 │
        ╞════════════════════╪═══════╡
        │ {"a": 1}           ┆ 1     │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ {}                 ┆ None  │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ {"b": 2}           ┆ None  │
        ╰────────────────────┴───────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    key_expr = Expression._to_expression(key)
    return Expression._from_pyexpr(expr._expr.map_get(key_expr._expr))