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

feat(python): Add lazy support for pl.select #20091

Merged
merged 2 commits into from
Dec 3, 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
47 changes: 43 additions & 4 deletions py-polars/polars/functions/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1874,25 +1874,47 @@ def collect_all_async(
return result


def select(*exprs: IntoExpr | Iterable[IntoExpr], **named_exprs: IntoExpr) -> DataFrame:
@overload
def select(
*exprs: IntoExpr | Iterable[IntoExpr],
eager: Literal[True] = ...,
**named_exprs: IntoExpr,
) -> DataFrame: ...


@overload
def select(
*exprs: IntoExpr | Iterable[IntoExpr],
eager: Literal[False],
**named_exprs: IntoExpr,
) -> LazyFrame: ...


def select(
*exprs: IntoExpr | Iterable[IntoExpr], eager: bool = True, **named_exprs: IntoExpr
) -> DataFrame | LazyFrame:
"""
Run polars expressions without a context.

This is syntactic sugar for running `df.select` on an empty DataFrame.
This is syntactic sugar for running `df.select` on an empty DataFrame
(or LazyFrame if eager=False).

Parameters
----------
*exprs
Column(s) to select, specified as positional arguments.
Accepts expression input. Strings are parsed as column names,
other non-expression inputs are parsed as literals.
eager
Evaluate immediately and return a `DataFrame` (default); if set to `False`,
return a `LazyFrame` instead.
**named_exprs
Additional columns to select, specified as keyword arguments.
The columns will be renamed to the keyword used.

Returns
-------
DataFrame
DataFrame or LazyFrame

Examples
--------
Expand All @@ -1909,8 +1931,25 @@ def select(*exprs: IntoExpr | Iterable[IntoExpr], **named_exprs: IntoExpr) -> Da
β”‚ 2 β”‚
β”‚ 1 β”‚
β””β”€β”€β”€β”€β”€β”˜

>>> pl.select(pl.int_range(0, 100_000, 2).alias("n"), eager=False).filter(
... pl.col("n") % 22_500 == 0
... ).collect()
shape: (5, 1)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”
β”‚ n β”‚
β”‚ --- β”‚
β”‚ i64 β”‚
β•žβ•β•β•β•β•β•β•β•‘
β”‚ 0 β”‚
β”‚ 22500 β”‚
β”‚ 45000 β”‚
β”‚ 67500 β”‚
β”‚ 90000 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”˜
"""
return pl.DataFrame().select(*exprs, **named_exprs)
empty_frame = pl.DataFrame() if eager else pl.LazyFrame()
return empty_frame.select(*exprs, **named_exprs)


@overload
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/functions/range/test_int_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def test_int_range_eager() -> None:
assert_series_equal(result, expected)


def test_int_range_lazy() -> None:
lf = pl.select(n=pl.int_range(8, 0, -2), eager=False)
expected = pl.LazyFrame({"n": [8, 6, 4, 2]})
assert_frame_equal(lf, expected)


def test_int_range_schema() -> None:
result = pl.LazyFrame().select(int=pl.int_range(-3, 3))

Expand Down
Loading