Skip to content

daft.functions.month#

month #

month(expr: Expression) -> Expression

Retrieves the month for a datetime column.

Returns:

Name Type Description
Expression Expression

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

Examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> import datetime
>>> import daft
>>> from daft.functions import month
>>> df = daft.from_pydict(
...     {
...         "datetime": [
...             datetime.datetime(2024, 7, 3, 0, 0, 0),
...             datetime.datetime(2024, 6, 4, 0, 0, 0),
...             datetime.datetime(2024, 5, 5, 0, 0, 0),
...         ],
...     }
... )
>>> df.with_column("month", month(df["datetime"])).collect()
╭─────────────────────┬────────╮
│ datetime            ┆ month  │
│ ---                 ┆ ---    │
│ Timestamp[us]       ┆ UInt32 │
╞═════════════════════╪════════╡
│ 2024-07-03 00:00:00 ┆ 7      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2024-06-04 00:00:00 ┆ 6      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2024-05-05 00:00:00 ┆ 5      │
╰─────────────────────┴────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/datetime.py
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
def month(expr: Expression) -> Expression:
    """Retrieves the month for a datetime column.

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

    Examples:
        >>> import datetime
        >>> import daft
        >>> from daft.functions import month
        >>> df = daft.from_pydict(
        ...     {
        ...         "datetime": [
        ...             datetime.datetime(2024, 7, 3, 0, 0, 0),
        ...             datetime.datetime(2024, 6, 4, 0, 0, 0),
        ...             datetime.datetime(2024, 5, 5, 0, 0, 0),
        ...         ],
        ...     }
        ... )
        >>> df.with_column("month", month(df["datetime"])).collect()
        ╭─────────────────────┬────────╮
        │ datetime            ┆ month  │
        │ ---                 ┆ ---    │
        │ Timestamp[us]       ┆ UInt32 │
        ╞═════════════════════╪════════╡
        │ 2024-07-03 00:00:00 ┆ 7      │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
        │ 2024-06-04 00:00:00 ┆ 6      │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
        │ 2024-05-05 00:00:00 ┆ 5      │
        ╰─────────────────────┴────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("month", expr)