Skip to content

Commit

Permalink
docs(python): Add date_range and datetime_ranges examples without `ea…
Browse files Browse the repository at this point in the history
…ger=True` (#18379)
  • Loading branch information
MarcoGorelli authored Aug 28, 2024
1 parent 22a97bf commit fb38919
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions py-polars/polars/functions/range/date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,36 @@ def date_range(
1985-01-07
1985-01-09
]
Omit `eager=True` if you want to use `date_range` as an expression:
>>> df = pl.DataFrame(
... {
... "date": [
... date(2024, 1, 1),
... date(2024, 1, 2),
... date(2024, 1, 1),
... date(2024, 1, 3),
... ],
... "key": ["one", "one", "two", "two"],
... }
... )
>>> result = (
... df.group_by("key")
... .agg(pl.date_range(pl.col("date").min(), pl.col("date").max()))
... .sort("key")
... )
>>> with pl.Config(fmt_str_lengths=50):
... print(result)
shape: (2, 2)
┌─────┬──────────────────────────────────────┐
│ key ┆ date │
│ --- ┆ --- │
│ str ┆ list[date] │
╞═════╪══════════════════════════════════════╡
│ one ┆ [2024-01-01, 2024-01-02] │
│ two ┆ [2024-01-01, 2024-01-02, 2024-01-03] │
└─────┴──────────────────────────────────────┘
"""
interval = parse_interval_argument(interval)

Expand Down
30 changes: 30 additions & 0 deletions py-polars/polars/functions/range/datetime_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,36 @@ def datetime_range(
2022-02-01 00:00:00 EST
2022-03-01 00:00:00 EST
]
Omit `eager=True` if you want to use `datetime_range` as an expression:
>>> df = pl.DataFrame(
... {
... "date": [
... date(2024, 1, 1),
... date(2024, 1, 2),
... date(2024, 1, 1),
... date(2024, 1, 3),
... ],
... "key": ["one", "one", "two", "two"],
... }
... )
>>> result = (
... df.group_by("key")
... .agg(pl.datetime_range(pl.col("date").min(), pl.col("date").max()))
... .sort("key")
... )
>>> with pl.Config(fmt_str_lengths=70):
... print(result)
shape: (2, 2)
┌─────┬─────────────────────────────────────────────────────────────────┐
│ key ┆ date │
│ --- ┆ --- │
│ str ┆ list[datetime[μs]] │
╞═════╪═════════════════════════════════════════════════════════════════╡
│ one ┆ [2024-01-01 00:00:00, 2024-01-02 00:00:00] │
│ two ┆ [2024-01-01 00:00:00, 2024-01-02 00:00:00, 2024-01-03 00:00:00] │
└─────┴─────────────────────────────────────────────────────────────────┘
"""
interval = parse_interval_argument(interval)
if time_unit is None and "ns" in interval:
Expand Down

0 comments on commit fb38919

Please sign in to comment.