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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
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))