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
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
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)