polars.Series.str.strptime#

Series.str.strptime(datatype: PolarsTemporalType, fmt: str | None = None, strict: bool = True, exact: bool = True, cache: bool = True, tz_aware: bool = False, utc: bool = False) 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.

cache

Use a cache of unique, converted dates to apply the datetime conversion.

tz_aware

Parse timezone aware datetimes. This may be automatically toggled by the ‘fmt’ given.

utc

Parse timezone aware datetimes as UTC. This may be useful if you have data with mixed offsets.

Returns:
A Date / Datetime / Time Series

Examples

Dealing with a consistent format:

>>> ts = ["2020-01-01 01:00Z", "2020-01-01 02:00Z"]
>>> pl.Series(ts).str.strptime(pl.Datetime, "%Y-%m-%d %H:%M%#z")
shape: (2,)
Series: '' [datetime[μs, +00:00]]
[
        2020-01-01 01:00:00 +00:00
        2020-01-01 02:00:00 +00:00
]

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