polars.internals.expr.ExprStringNameSpace.strptime

ExprStringNameSpace.strptime(datatype: Union[Type[polars.datatypes.Date], Type[polars.datatypes.Datetime]], fmt: Optional[str] = None, strict: bool = True, exact: bool = True) polars.internals.expr.Expr

Parse utf8 expression as a Date/Datetimetype.

Parameters
datatype

Date | Datetime.

fmt

format to use, see the following link for examples: https://docs.rs/chrono/latest/chrono/format/strftime/index.html

example: “%y-%m-%d”.

strict

raise an error if any conversion fails

exact
  • If True, require an exact format match.

  • If False, allow the format to match anywhere in the target string.

Examples

Dealing with different formats.

>>> s = pl.Series(
...     "date",
...     [
...         "2021-04-22",
...         "2022-01-04 00:00:00",
...         "01/31/22",
...         "Sun Jul  8 00:34:60 2001",
...     ],
... )
>>> (
...     s.to_frame().with_column(
...         pl.col("date")
...         .str.strptime(pl.Date, "%F", strict=False)
...         .fill_null(
...             pl.col("date").str.strptime(pl.Date, "%F %T", strict=False)
...         )
...         .fill_null(pl.col("date").str.strptime(pl.Date, "%D", strict=False))
...         .fill_null(pl.col("date").str.strptime(pl.Date, "%c", strict=False))
...     )
... )
shape: (4, 1)
┌────────────┐
│ date       │
│ ---        │
│ date       │
╞════════════╡
│ 2021-04-22 │
├╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2022-01-04 │
├╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2022-01-31 │
├╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 2001-07-08 │
└────────────┘