Skip to content

daft.functions.datepart#

datepart #

datepart(part: str, expr: Expression) -> Expression

Alias-style extractor over existing temporal functions.

Parameters:

Name Type Description Default
part str

Date part name, e.g. "year", "dayofmonth", "weekofyear".

required
expr Expression

Temporal expression to extract from.

required
Source code in daft/functions/datetime.py
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
def datepart(part: str, expr: Expression) -> Expression:
    """Alias-style extractor over existing temporal functions.

    Args:
        part: Date part name, e.g. ``"year"``, ``"dayofmonth"``, ``"weekofyear"``.
        expr: Temporal expression to extract from.
    """
    normalized = part.strip().lower().replace(" ", "_")
    extractors = {
        "date": date,
        "year": year,
        "quarter": quarter,
        "month": month,
        "day": day,
        "day_of_week": day_of_week,
        "dayofweek": day_of_week,
        "weekday": day_of_week,
        "day_of_month": day_of_month,
        "dayofmonth": day_of_month,
        "day_of_year": day_of_year,
        "dayofyear": day_of_year,
        "week_of_year": week_of_year,
        "weekofyear": week_of_year,
        "week": week_of_year,
        "hour": hour,
        "minute": minute,
        "second": second,
        "millisecond": millisecond,
        "microsecond": microsecond,
        "nanosecond": nanosecond,
        "unix_date": unix_date,
    }
    if normalized not in extractors:
        supported = ", ".join(sorted(extractors))
        raise ValueError(f"Unsupported datepart value: {part!r}. Supported values: {supported}")
    return extractors[normalized](expr)