Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(python): Add date_range and datetime_ranges examples without eager=True #18379

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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