Skip to content

daft.functions.strftime#

strftime #

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

Converts a datetime/date column to a string column.

Parameters:

Name Type Description Default
expr Expression

The datetime or date expression to convert.

required
format str | None

The format to use for the conversion. If None, defaults to ISO 8601 format.

None
Note

The format must be a valid datetime format string. (defaults to ISO 8601 format) See: https://docs.rs/chrono/latest/chrono/format/strftime/index.html

Examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
>>> import datetime
>>> import daft
>>> from daft.functions import strftime
>>> df = daft.from_pydict(
...     {
...         "dates": [datetime.date(2023, 1, 1), datetime.date(2023, 1, 2), datetime.date(2023, 1, 3)],
...         "datetimes": [
...             datetime.datetime(2023, 1, 1, 12, 1),
...             datetime.datetime(2023, 1, 2, 12, 0, 0, 0),
...             datetime.datetime(2023, 1, 3, 12, 0, 0, 999_999),
...         ],
...     }
... )
>>> df = df.with_column("datetimes_s", df["datetimes"].cast(daft.DataType.timestamp("s")))
>>> df.select(
...     strftime(df["dates"]).alias("iso_date"),
...     strftime(df["dates"], format="%m/%d/%Y").alias("custom_date"),
...     strftime(df["datetimes"]).alias("iso_datetime"),
...     strftime(df["datetimes_s"]).alias("iso_datetime_s"),
...     strftime(df["datetimes_s"], format="%Y/%m/%d %H:%M:%S").alias("custom_datetime"),
... ).show()
╭────────────┬─────────────┬────────────────────────────┬─────────────────────┬─────────────────────╮
│ iso_date   ┆ custom_date ┆ iso_datetime               ┆ iso_datetime_s      ┆ custom_datetime     │
│ ---        ┆ ---         ┆ ---                        ┆ ---                 ┆ ---                 │
│ String     ┆ String      ┆ String                     ┆ String              ┆ String              │
╞════════════╪═════════════╪════════════════════════════╪═════════════════════╪═════════════════════╡
│ 2023-01-01 ┆ 01/01/2023  ┆ 2023-01-01T12:01:00        ┆ 2023-01-01T12:01:00 ┆ 2023/01/01 12:01:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2023-01-02 ┆ 01/02/2023  ┆ 2023-01-02T12:00:00        ┆ 2023-01-02T12:00:00 ┆ 2023/01/02 12:00:00 │
├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2023-01-03 ┆ 01/03/2023  ┆ 2023-01-03T12:00:00.999999 ┆ 2023-01-03T12:00:00 ┆ 2023/01/03 12:00:00 │
╰────────────┴─────────────┴────────────────────────────┴─────────────────────┴─────────────────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/datetime.py
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
def strftime(expr: Expression, format: str | None = None) -> Expression:
    """Converts a datetime/date column to a string column.

    Args:
        expr: The datetime or date expression to convert.
        format: The format to use for the conversion. If None, defaults to ISO 8601 format.

    Note:
        The format must be a valid datetime format string. (defaults to ISO 8601 format)
        See: https://docs.rs/chrono/latest/chrono/format/strftime/index.html


    Examples:
        >>> import datetime
        >>> import daft
        >>> from daft.functions import strftime
        >>> df = daft.from_pydict(
        ...     {
        ...         "dates": [datetime.date(2023, 1, 1), datetime.date(2023, 1, 2), datetime.date(2023, 1, 3)],
        ...         "datetimes": [
        ...             datetime.datetime(2023, 1, 1, 12, 1),
        ...             datetime.datetime(2023, 1, 2, 12, 0, 0, 0),
        ...             datetime.datetime(2023, 1, 3, 12, 0, 0, 999_999),
        ...         ],
        ...     }
        ... )
        >>> df = df.with_column("datetimes_s", df["datetimes"].cast(daft.DataType.timestamp("s")))
        >>> df.select(
        ...     strftime(df["dates"]).alias("iso_date"),
        ...     strftime(df["dates"], format="%m/%d/%Y").alias("custom_date"),
        ...     strftime(df["datetimes"]).alias("iso_datetime"),
        ...     strftime(df["datetimes_s"]).alias("iso_datetime_s"),
        ...     strftime(df["datetimes_s"], format="%Y/%m/%d %H:%M:%S").alias("custom_datetime"),
        ... ).show()
        ╭────────────┬─────────────┬────────────────────────────┬─────────────────────┬─────────────────────╮
        │ iso_date   ┆ custom_date ┆ iso_datetime               ┆ iso_datetime_s      ┆ custom_datetime     │
        │ ---        ┆ ---         ┆ ---                        ┆ ---                 ┆ ---                 │
        │ String     ┆ String      ┆ String                     ┆ String              ┆ String              │
        ╞════════════╪═════════════╪════════════════════════════╪═════════════════════╪═════════════════════╡
        │ 2023-01-01 ┆ 01/01/2023  ┆ 2023-01-01T12:01:00        ┆ 2023-01-01T12:01:00 ┆ 2023/01/01 12:01:00 │
        ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2023-01-02 ┆ 01/02/2023  ┆ 2023-01-02T12:00:00        ┆ 2023-01-02T12:00:00 ┆ 2023/01/02 12:00:00 │
        ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 2023-01-03 ┆ 01/03/2023  ┆ 2023-01-03T12:00:00.999999 ┆ 2023-01-03T12:00:00 ┆ 2023/01/03 12:00:00 │
        ╰────────────┴─────────────┴────────────────────────────┴─────────────────────┴─────────────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)
    """
    return Expression._call_builtin_scalar_fn("strftime", expr, format)