polars.Expr.str.strptime#
- Expr.str.strptime(datatype: PolarsTemporalType, fmt: str | None = None, strict: bool = True, exact: bool = True, cache: bool = True, tz_aware: bool | NoDefault = _NoDefault.no_default, utc: bool = False) Expr [source]#
Parse a Utf8 expression to a Date/Datetime/Time type.
- Parameters:
- datatype
Date | Datetime | 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.
Deprecated since version 0.16.17: This is now auto-inferred from the given fmt. You can safely drop this argument, it will be removed in a future version.
- utc
Parse timezone aware datetimes as UTC. This may be useful if you have data with mixed offsets.
Notes
When parsing a Datetime the column precision will be inferred from the format string, if given, eg: “%F %T%.3f” => Datetime(“ms”). If no fractional second component is found then the default is “us”.
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 │ └────────────┘