Skip to content

daft.functions.count#

count #

count(expr: Expression | None = None, mode: Literal['all', 'valid', 'null'] | CountMode = Valid) -> Expression

Counts the number of values in the expression.

Parameters:

Name Type Description Default
expr Expression | None

The input expression to count values from. If not provided, mode must be "all" and count(*) semantics will be used.

None
mode Literal['all', 'valid', 'null'] | CountMode

A string ("all", "valid", or "null") that represents whether to count all values, non-null (valid) values, or null values. Defaults to "valid".

Valid
Source code in daft/functions/agg.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def count(
    expr: Expression | None = None, mode: Literal["all", "valid", "null"] | CountMode = CountMode.Valid
) -> Expression:
    """Counts the number of values in the expression.

    Args:
        expr (Expression | None): The input expression to count values from. If not provided, mode must be "all"
            and count(*) semantics will be used.
        mode: A string ("all", "valid", or "null") that represents whether to count all values, non-null (valid) values, or null values. Defaults to "valid".
    """
    if isinstance(mode, str):
        mode = CountMode.from_count_mode_str(mode)
    if expr is None:
        if mode != CountMode.All:
            raise ValueError("count() without an expression only supports mode='all'.")
        expr = col("*")
    return Expression._from_pyexpr(expr._expr.count(mode))