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
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
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)