Skip to content

Convert a String column into a Date column

Source code

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.
Not used.
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.

Format

Format to use for conversion. Refer to the chrono crate documentation for the full specification. Example: “%Y-%m-%d”. If NULL (default), the format is inferred from the data.

Value

Expr of Date type

See Also

  • \$str$strptime()

Examples

library(polars)

s = as_polars_series(c("2020/01/01", "2020/02/01", "2020/03/01"))

s$str$to_date()
#> polars Series: shape: (3,)
#> Series: '' [date]
#> [
#>  2020-01-01
#>  2020-02-01
#>  2020-03-01
#> ]
# by default, this errors if some values cannot be converted
s = as_polars_series(c("2020/01/01", "2020 02 01", "2020-03-01"))
try(s$str$to_date())
#> Error : Execution halted with the following contexts
#>    0: In R: in $select()
#>    0: During function call [.main()]
#>    1: Encountered the following error in Rust-Polars:
#>          conversion from `str` to `date` failed in column '' 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
s$str$to_date(strict = FALSE)
#> polars Series: shape: (3,)
#> Series: '' [date]
#> [
#>  2020-01-01
#>  null
#>  2020-03-01
#> ]