Skip to content

daft.functions.trunc#

trunc #

trunc(expr: Expression, interval: str, relative_to: Expression | None = None) -> Expression

Alias for date_trunc with Spark-style argument order.

Parameters:

Name Type Description Default
expr Expression

The datetime/date expression to truncate.

required
interval str

The truncation unit/interval (e.g. "day", "month", "1 hour").

required
relative_to optional

Timestamp to truncate relative to.

None
Source code in daft/functions/datetime.py
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
def trunc(expr: Expression, interval: str, relative_to: Expression | None = None) -> Expression:
    """Alias for ``date_trunc`` with Spark-style argument order.

    Args:
        expr: The datetime/date expression to truncate.
        interval: The truncation unit/interval (e.g. ``"day"``, ``"month"``, ``"1 hour"``).
        relative_to (optional): Timestamp to truncate relative to.
    """
    normalized_interval = interval
    stripped = interval.strip()
    if stripped and not stripped[0].isdigit():
        normalized_interval = f"1 {stripped}"
    return date_trunc(normalized_interval, expr, relative_to)