Skip to content

Convert a Date/Time/Datetime/Duration column into a String column with the given format

Description

Similar to $cast(pl$String), but this method allows you to customize the formatting of the resulting string. This is an alias for $dt$to_string().

Usage

<Expr>$dt$strftime(format)

Arguments

format Single string of format to use, or NULL. NULL will be treated as “iso”. Available formats depend on the column data type:
  • For Date/Time/Datetime, refer to the chrono strftime documentation for specification. Example: “%y-%m-%d”. Special case “iso” will use the ISO8601 format.
  • For Duration, “iso” or “polars” can be used. The “iso” format string results in ISO8601 duration string output, and “polars” results in the same form seen in the polars print representation.

Value

A polars expression

Examples

library("polars")

pl$DataFrame(
  datetime = c(as.POSIXct(c("2021-01-02 00:00:00", "2021-01-03 00:00:00")))
)$
  with_columns(
  datetime_string = pl$col("datetime")$dt$strftime("%Y/%m/%d %H:%M:%S")
)
#> shape: (2, 2)
#> ┌─────────────────────┬─────────────────────┐
#> │ datetime            ┆ datetime_string     │
#> │ ---                 ┆ ---                 │
#> │ datetime[ms]        ┆ str                 │
#> ╞═════════════════════╪═════════════════════╡
#> │ 2021-01-02 00:00:00 ┆ 2021/01/02 00:00:00 │
#> │ 2021-01-03 00:00:00 ┆ 2021/01/03 00:00:00 │
#> └─────────────────────┴─────────────────────┘