Skip to content

daft.functions.from_unixtime#

from_unixtime #

from_unixtime(expr: Expression, format: str | None = None) -> Expression

Converts a Unix timestamp (seconds) to a formatted string.

Parameters:

Name Type Description Default
expr Expression

A numeric expression representing seconds since epoch.

required
format str | None

Optional strftime format string. Defaults to '%Y-%m-%d %H:%M:%S'.

None

Returns:

Name Type Description
Expression Expression

a Utf8 expression with the formatted timestamp.

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import from_unixtime
>>> df = daft.from_pydict({"s": [0, 1609459200]})
>>> df = df.with_column("formatted", from_unixtime(df["s"]))
>>> df.schema()["formatted"].dtype == daft.DataType.string()
True
Source code in daft/functions/datetime.py
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
def from_unixtime(expr: Expression, format: str | None = None) -> Expression:
    """Converts a Unix timestamp (seconds) to a formatted string.

    Args:
        expr: A numeric expression representing seconds since epoch.
        format: Optional strftime format string. Defaults to '%Y-%m-%d %H:%M:%S'.

    Returns:
        Expression: a Utf8 expression with the formatted timestamp.

    Examples:
        >>> import daft
        >>> from daft.functions import from_unixtime
        >>> df = daft.from_pydict({"s": [0, 1609459200]})
        >>> df = df.with_column("formatted", from_unixtime(df["s"]))
        >>> df.schema()["formatted"].dtype == daft.DataType.string()
        True
    """
    if format is not None:
        return Expression._call_builtin_scalar_fn("from_unixtime", expr, format=format)
    return Expression._call_builtin_scalar_fn("from_unixtime", expr)