Skip to content

Convert a String column into a Datetime column

Source code

Description

Convert a String column into a Datetime column

Usage

<Expr>$str$to_datetime(
  format = NULL,
  ...,
  time_unit = NULL,
  time_zone = NULL,
  strict = TRUE,
  exact = TRUE,
  cache = TRUE,
  ambiguous = "raise"
)

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.
time_unit Unit of time for the resulting Datetime column. If NULL (default), the time unit is inferred from the format string if given, e.g.: “%F %T%.3f” =\> pl$Datetime("ms"). If no fractional second component is found, the default is “us” (microsecond).
time_zone for the resulting Datetime column.
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. 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.
ambiguous Determine how to deal with ambiguous datetimes:
  • “raise” (default): throw an error
  • “earliest”: use the earliest datetime
  • “latest”: use the latest datetime
  • “null”: return a null value

Value

Expr of Datetime type

See Also

  • \$str$strptime()

Examples

library("polars")

s = as_polars_series(c("2020-01-01 01:00Z", "2020-01-01 02:00Z"))

s$str$to_datetime("%Y-%m-%d %H:%M%#z")
#> polars Series: shape: (2,)
#> Series: '' [datetime[μs, UTC]]
#> [
#>  2020-01-01 01:00:00 UTC
#>  2020-01-01 02:00:00 UTC
#> ]
s$str$to_datetime(time_unit = "ms")
#> polars Series: shape: (2,)
#> Series: '' [datetime[ms, UTC]]
#> [
#>  2020-01-01 01:00:00 UTC
#>  2020-01-01 02:00:00 UTC
#> ]