:wave: how much work would it be to add `mode` (mo...
# general
g
👋 how much work would it be to add
mode
(most common value) to aggregations?
also how could I apply the same UDF to multiple columns with
map_groups
?as a workaround for computing
mode
c
how much work would it be to add
mode
(most common value)
Probably be quite involved, medium-hard difficulty. Mostly due to the fact that it would require an implementation for both local and distributed cases.
apply the same UDF to multiple columns with
map_groups
You can pass multiple columns to the UDF in map groups, and also return multiple columns. Example
Copy code
import daft
import statistics

from daft.functions.struct import unnest


@daft.udf(
    return_dtype=daft.DataType.struct(
        {"mode1": daft.DataType.int64(), "mode2": daft.DataType.int64()}
    )
)
def mode(data1, data2):
    return [
        {
            "mode1": statistics.mode(data1),
            "mode2": statistics.mode(data2),
        }
    ]


df = daft.from_pydict(
    {
        "group": ["a", "a", "a", "b", "b", "b"],
        "data1": [1, 2, 2, 3, 3, 4],
        "data2": [1, 1, 2, 3, 4, 4],
    }
)

df = (
    df.groupby("group")
    .map_groups(mode(daft.col("data1"), daft.col("data2")).alias("mode"))
    .select("group", unnest(daft.col("mode")))
)
print(df.collect())
g
got it and makes sense that
mode
will be tedious to implement. I figured I could pass multiple columns, but wanted to see if I could avoid