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
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
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)