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
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
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)