Skip to content

daft.functions.date_trunc#

date_trunc #

date_trunc(interval: str, expr: Expression, relative_to: Expression | None = None) -> Expression

Truncates the datetime column to the specified interval.

Parameters:

Name Type Description Default
interval str

The interval to truncate to. Must be a string representing a valid interval in "{integer} {unit}" format, e.g. "1 day". Valid time units are: 'microsecond', 'millisecond', 'second', 'minute', 'hour', 'day', 'week'.

required
expr Expression

The datetime expression to truncate.

required
relative_to optional

Timestamp to truncate relative to. If not provided, truncates to the start of the Unix epoch: 1970-01-01 00:00:00.

None

Returns:

Name Type Description
Expression Expression

a DateTime expression truncated to the specified interval

Examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> import datetime
>>> import daft
>>> from daft.functions import date_trunc
>>>
>>> df = daft.from_pydict(
...     {
...         "datetime": [
...             datetime.datetime(2021, 1, 1, 0, 1, 1),
...             datetime.datetime(2021, 1, 1, 0, 1, 59),
...             datetime.datetime(2021, 1, 1, 0, 2, 0),
...         ],
...     }
... )
>>> df.with_column("truncated", date_trunc("1 minute", df["datetime"])).collect()
╭─────────────────────┬─────────────────────╮
│ datetime            ┆ truncated           │
│ ---                 ┆ ---                 │
│ Timestamp[us]       ┆ Timestamp[us]       │
╞═════════════════════╪═════════════════════╡
│ 2021-01-01 00:01:01 ┆ 2021-01-01 00:01:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2021-01-01 00:01:59 ┆ 2021-01-01 00:01:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2021-01-01 00:02:00 ┆ 2021-01-01 00:02:00 │
╰─────────────────────┴─────────────────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/datetime.py
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
def date_trunc(interval: str, expr: Expression, relative_to: Expression | None = None) -> Expression:
    """Truncates the datetime column to the specified interval.

    Args:
        interval: The interval to truncate to. Must be a string representing a valid interval in "{integer} {unit}" format, e.g. "1 day". Valid time units are: 'microsecond', 'millisecond', 'second', 'minute', 'hour', 'day', 'week'.
        expr: The datetime expression to truncate.
        relative_to (optional): Timestamp to truncate relative to. If not provided, truncates to the start of the Unix epoch: 1970-01-01 00:00:00.

    Returns:
        Expression: a DateTime expression truncated to the specified interval

    Examples:
        >>> import datetime
        >>> import daft
        >>> from daft.functions import date_trunc
        >>>
        >>> df = daft.from_pydict(
        ...     {
        ...         "datetime": [
        ...             datetime.datetime(2021, 1, 1, 0, 1, 1),
        ...             datetime.datetime(2021, 1, 1, 0, 1, 59),
        ...             datetime.datetime(2021, 1, 1, 0, 2, 0),
        ...         ],
        ...     }
        ... )
        >>> df.with_column("truncated", date_trunc("1 minute", df["datetime"])).collect()
        ╭─────────────────────┬─────────────────────╮
        │ datetime            ┆ truncated           │
        │ ---                 ┆ ---                 │
        │ Timestamp[us]       ┆ Timestamp[us]       │
        ╞═════════════════════╪═════════════════════╡
        │ 2021-01-01 00:01:01 ┆ 2021-01-01 00:01:00 │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2021-01-01 00:01:59 ┆ 2021-01-01 00:01:00 │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2021-01-01 00:02:00 ┆ 2021-01-01 00:02:00 │
        ╰─────────────────────┴─────────────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("truncate", expr, relative_to, interval=interval)