Skip to content

daft.functions.to_unix_epoch#

to_unix_epoch #

to_unix_epoch(expr: Expression, time_unit: str | TimeUnit | None = None) -> Expression

Converts a datetime column to a Unix timestamp with the specified time unit. (default: seconds).

See daft.datatype.TimeUnit for more information on time units and valid values.

Examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> import daft
>>> from datetime import date
>>>
>>> df = daft.from_pydict(
...     {
...         "dates": [
...             date(2001, 1, 1),
...             date(2001, 1, 2),
...             date(2001, 1, 3),
...             None,
...         ]
...     }
... )
>>> df.with_column("timestamp", df["dates"].to_unix_epoch("ns")).show()
╭────────────┬────────────────────╮
│ dates      ┆ timestamp          │
│ ---        ┆ ---                │
│ Date       ┆ Int64              │
╞════════════╪════════════════════╡
│ 2001-01-01 ┆ 978307200000000000 │
├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-01-02 ┆ 978393600000000000 │
├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-01-03 ┆ 978480000000000000 │
├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ None       ┆ None               │
╰────────────┴────────────────────╯
(Showing first 4 of 4 rows)
Source code in daft/functions/datetime.py
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
def to_unix_epoch(expr: Expression, time_unit: str | TimeUnit | None = None) -> Expression:
    """Converts a datetime column to a Unix timestamp with the specified time unit. (default: seconds).

    See [daft.datatype.TimeUnit](https://docs.daft.ai/en/stable/api/datatypes/all_datatypes/#daft.datatype.DataType.timeunit) for more information on time units and valid values.

    Examples:
        >>> import daft
        >>> from datetime import date
        >>>
        >>> df = daft.from_pydict(
        ...     {
        ...         "dates": [
        ...             date(2001, 1, 1),
        ...             date(2001, 1, 2),
        ...             date(2001, 1, 3),
        ...             None,
        ...         ]
        ...     }
        ... )
        >>> df.with_column("timestamp", df["dates"].to_unix_epoch("ns")).show()
        ╭────────────┬────────────────────╮
        │ dates      ┆ timestamp          │
        │ ---        ┆ ---                │
        │ Date       ┆ Int64              │
        ╞════════════╪════════════════════╡
        │ 2001-01-01 ┆ 978307200000000000 │
        ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2001-01-02 ┆ 978393600000000000 │
        ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2001-01-03 ┆ 978480000000000000 │
        ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ None       ┆ None               │
        ╰────────────┴────────────────────╯
        <BLANKLINE>
        (Showing first 4 of 4 rows)
    """
    return Expression._call_builtin_scalar_fn("to_unix_epoch", expr, time_unit=time_unit)