Garrett Weaver
11/20/2025, 9:11 PMmode (most common value) to aggregations?Garrett Weaver
11/20/2025, 9:45 PMmap_groups?as a workaround for computing modeColin Ho
11/21/2025, 12:50 PMhow much work would it be to addProbably be quite involved, medium-hard difficulty. Mostly due to the fact that it would require an implementation for both local and distributed cases.(most common value)mode
apply the same UDF to multiple columns withYou can pass multiple columns to the UDF in map groups, and also return multiple columns. Examplemap_groups
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())Garrett Weaver
11/22/2025, 12:01 AMmode will be tedious to implement. I figured I could pass multiple columns, but wanted to see if I could avoid