Skip to content

daft.functions.map_keys#

map_keys #

map_keys(expr: Expression) -> Expression

Returns a list of all keys in the map.

Parameters:

Name Type Description Default
expr Expression

the map expression to get from

required

Returns:

Name Type Description
Expression Expression

the keys list expression

Examples:

1
2
3
4
5
6
>>> import pyarrow as pa
>>> import daft
>>> pa_array = pa.array([[("a", 1), ("b", 2)], [("c", 3)], [], None], type=pa.map_(pa.string(), pa.int64()))
>>> df = daft.from_arrow(pa.table({"map_col": pa_array}))
>>> df = df.with_column("keys", df["map_col"].map_keys())
>>> df.show()
╭────────────────────┬──────────────╮
│ map_col            ┆ keys         │
│ ---                ┆ ---          │
│ Map[String: Int64] ┆ List[String] │
╞════════════════════╪══════════════╡
│ {"a": 1, "b": 2}   ┆ [a, b]       │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ {"c": 3}           ┆ [c]          │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ {}                 ┆ None         │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ None               ┆ None         │
╰────────────────────┴──────────────╯
(Showing first 4 of 4 rows)
Source code in daft/functions/misc.py
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
def map_keys(expr: Expression) -> Expression:
    """Returns a list of all keys in the map.

    Args:
        expr: the map expression to get from

    Returns:
        Expression: the keys list expression

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

    """
    return Expression._from_pyexpr(expr._expr.map_keys())