Skip to content

Convert a String column into a Date column

Description

Convert a String column into a Date column

Usage

<Expr>$str$to_date(format = NULL, ..., strict = TRUE, exact = TRUE, cache = TRUE)

Arguments

format Format to use for conversion. Refer to the chrono crate documentation for the full specification. Example: “%Y-%m-%d %H:%M:%S”. If NULL (default), the format is inferred from the data. Notice that time zone %Z is not supported and will just ignore timezones. Numeric time zones like %z or %:z are supported.
These dots are for future extensions and must be empty.
strict If TRUE (default), raise an error if a single string cannot be parsed. If FALSE, produce a polars null.
exact If TRUE (default), require an exact format match. If FALSE, allow the format to match anywhere in the target string. Conversion to the Time type is always exact. Note that using exact = FALSE introduces a performance penalty - cleaning your data beforehand will almost certainly be more performant.
cache Use a cache of unique, converted dates to apply the datetime conversion.

Value

A polars expression

See Also

  • \$str$strptime()

Examples

library("polars")

df <- pl$DataFrame(x = c("2020/01/01", "2020/02/01", "2020/03/01"))

df$select(pl$col("x")$str$to_date())
#> shape: (3, 1)
#> ┌────────────┐
#> │ x          │
#> │ ---        │
#> │ date       │
#> ╞════════════╡
#> │ 2020-01-01 │
#> │ 2020-02-01 │
#> │ 2020-03-01 │
#> └────────────┘
# by default, this errors if some values cannot be converted
df <- pl$DataFrame(x = c("2020/01/01", "2020 02 01", "2020-03-01"))
try(df$select(pl$col("x")$str$to_date()))
#> Error in df$select(pl$col("x")$str$to_date()) : 
#>   Evaluation failed in `$select()`.
#> Caused by error:
#> ! Evaluation failed in `$collect()`.
#> Caused by error:
#> ! Invalid operation: conversion from `str` to `date` failed in column 'x' for 1 out of 3 values: ["2020 02 01"]
#> 
#> You might want to try:
#> - setting `strict=False` to set values that cannot be converted to `null`
#> - using `str.strptime`, `str.to_date`, or `str.to_datetime` and providing a format string
df$select(pl$col("x")$str$to_date(strict = FALSE))
#> shape: (3, 1)
#> ┌────────────┐
#> │ x          │
#> │ ---        │
#> │ date       │
#> ╞════════════╡
#> │ 2020-01-01 │
#> │ null       │
#> │ 2020-03-01 │
#> └────────────┘