Skip to content

daft.functions.hour#

hour #

hour(expr: Expression) -> Expression

Retrieves the hour for a datetime column.

Returns:

Name Type Description
Expression Expression

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

Examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> import datetime
>>> import daft
>>> from daft.functions import hour
>>> df = daft.from_pydict(
...     {
...         "x": [
...             datetime.datetime(2021, 1, 1, 5, 1, 1),
...             datetime.datetime(2021, 1, 2, 6, 1, 59),
...             datetime.datetime(2021, 1, 3, 7, 2, 0),
...         ],
...     }
... )
>>> df = df.with_column("hour", hour(df["x"]))
>>> df.show()
╭─────────────────────┬────────╮
│ x                   ┆ hour   │
│ ---                 ┆ ---    │
│ Timestamp[us]       ┆ UInt32 │
╞═════════════════════╪════════╡
│ 2021-01-01 05:01:01 ┆ 5      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2021-01-02 06:01:59 ┆ 6      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2021-01-03 07:02:00 ┆ 7      │
╰─────────────────────┴────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/datetime.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
def hour(expr: Expression) -> Expression:
    """Retrieves the hour for a datetime column.

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

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

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