Skip to content

daft.functions.to_date#

to_date #

to_date(expr: Expression, format: str) -> Expression

Converts a string to a date using the specified format.

Returns:

Name Type Description
Expression Expression

a Date expression which is parsed by given format

Note

The format must be a valid date format string. See: https://docs.rs/chrono/latest/chrono/format/strftime/index.html

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import to_date
>>> df = daft.from_pydict({"x": ["2021-01-01", "2021-01-02", None]})
>>> df = df.with_column("date", to_date(df["x"], "%Y-%m-%d"))
>>> df.show()
╭────────────┬────────────╮
│ x          ┆ date       │
│ ---        ┆ ---        │
│ String     ┆ Date       │
╞════════════╪════════════╡
│ 2021-01-01 ┆ 2021-01-01 │
├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2021-01-02 ┆ 2021-01-02 │
├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ None       ┆ None       │
╰────────────┴────────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/datetime.py
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
def to_date(expr: Expression, format: str) -> Expression:
    """Converts a string to a date using the specified format.

    Returns:
        Expression: a Date expression which is parsed by given format

    Note:
        The format must be a valid date format string. See: https://docs.rs/chrono/latest/chrono/format/strftime/index.html

    Examples:
        >>> import daft
        >>> from daft.functions import to_date
        >>> df = daft.from_pydict({"x": ["2021-01-01", "2021-01-02", None]})
        >>> df = df.with_column("date", to_date(df["x"], "%Y-%m-%d"))
        >>> df.show()
        ╭────────────┬────────────╮
        │ x          ┆ date       │
        │ ---        ┆ ---        │
        │ String     ┆ Date       │
        ╞════════════╪════════════╡
        │ 2021-01-01 ┆ 2021-01-01 │
        ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2021-01-02 ┆ 2021-01-02 │
        ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ None       ┆ None       │
        ╰────────────┴────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("to_date", expr, format=format)