Skip to content

Commit

Permalink
python add example to strptime documentation (#2717)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Feb 20, 2022
1 parent f796322 commit 03924e0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
40 changes: 40 additions & 0 deletions py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2555,6 +2555,46 @@ def strptime(
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 │
└────────────┘
"""
if not issubclass(datatype, DataType):
raise ValueError(
Expand Down
41 changes: 41 additions & 0 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3507,7 +3507,48 @@ def strptime(
Returns
-------
A Date/ Datetime 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 │
└────────────┘
"""

s = wrap_s(self._s)
return (
s.to_frame()
Expand Down

0 comments on commit 03924e0

Please sign in to comment.