polars.internals.series.string.StringNameSpace.strptime#

StringNameSpace.strptime(datatype: Union[Type[Datetime], Datetime, Type[Date], Date, Type[Time], Time], fmt: str | None = None, strict: bool = True, exact: bool = True) Series[source]#

Parse a Series of dtype Utf8 to a Date/Datetime Series.

Parameters:
datatype

Date, Datetime or Time.

fmt

Format to use, refer to the chrono strftime documentation for specification. 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.

Returns:
A Date / Datetime / Time Series

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 │
└────────────┘