Skip to content

daft.functions.week_of_year#

week_of_year #

week_of_year(expr: Expression) -> Expression

Retrieves the week of the year for a datetime column.

Returns:

Name Type Description
Expression Expression

a UInt32 expression with just the week_of_year extracted from a datetime column

Examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
>>> import datetime
>>> import daft
>>> from daft.functions import week_of_year
>>> df = daft.from_pydict(
...     {
...         "datetime": [
...             datetime.datetime(2024, 1, 1, 0, 0, 0),
...             datetime.datetime(2024, 2, 1, 0, 0, 0),
...             datetime.datetime(
...                 2024, 12, 31, 0, 0, 0
...             ),  # part of week 1 of 2025 according to ISO 8601 standard
...             datetime.datetime(2023, 12, 31, 0, 0, 0),
...         ],
...     }
... )
>>> df.with_column("week_of_year", week_of_year(df["datetime"])).collect()
╭─────────────────────┬──────────────╮
│ datetime            ┆ week_of_year │
│ ---                 ┆ ---          │
│ Timestamp[us]       ┆ UInt32       │
╞═════════════════════╪══════════════╡
│ 2024-01-01 00:00:00 ┆ 1            │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2024-02-01 00:00:00 ┆ 5            │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2024-12-31 00:00:00 ┆ 1            │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2023-12-31 00:00:00 ┆ 52           │
╰─────────────────────┴──────────────╯
(Showing first 4 of 4 rows)
Source code in daft/functions/datetime.py
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
def week_of_year(expr: Expression) -> Expression:
    """Retrieves the week of the year for a datetime column.

    Returns:
        Expression: a UInt32 expression with just the week_of_year extracted from a datetime column

    Examples:
        >>> import datetime
        >>> import daft
        >>> from daft.functions import week_of_year
        >>> df = daft.from_pydict(
        ...     {
        ...         "datetime": [
        ...             datetime.datetime(2024, 1, 1, 0, 0, 0),
        ...             datetime.datetime(2024, 2, 1, 0, 0, 0),
        ...             datetime.datetime(
        ...                 2024, 12, 31, 0, 0, 0
        ...             ),  # part of week 1 of 2025 according to ISO 8601 standard
        ...             datetime.datetime(2023, 12, 31, 0, 0, 0),
        ...         ],
        ...     }
        ... )
        >>> df.with_column("week_of_year", week_of_year(df["datetime"])).collect()
        ╭─────────────────────┬──────────────╮
        │ datetime            ┆ week_of_year │
        │ ---                 ┆ ---          │
        │ Timestamp[us]       ┆ UInt32       │
        ╞═════════════════════╪══════════════╡
        │ 2024-01-01 00:00:00 ┆ 1            │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2024-02-01 00:00:00 ┆ 5            │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2024-12-31 00:00:00 ┆ 1            │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2023-12-31 00:00:00 ┆ 52           │
        ╰─────────────────────┴──────────────╯
        <BLANKLINE>
        (Showing first 4 of 4 rows)
    """
    return Expression._call_builtin_scalar_fn("week_of_year", expr)