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

fix(python): Raise properly for slices not supported by LazyFrame #15331

Merged
merged 2 commits into from
Mar 27, 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
5 changes: 3 additions & 2 deletions py-polars/polars/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def apply(self, s: slice) -> LazyFrame:
# [i:<=i]
# [i:>=i:-k]
if (step > 0 and (s.stop is not None and start >= s.stop)) or (
step < 0 and (s.stop is not None and s.stop >= s.start >= 0)
step < 0
and (s.start is not None and s.stop is not None and s.stop >= s.start >= 0)
):
return self.obj.clear()

Expand Down Expand Up @@ -192,7 +193,7 @@ def apply(self, s: slice) -> LazyFrame:
# ---------------------------------------
# [-i:] => tail(abs(i))
# [-i::k] => tail(abs(i)).gather_every(k)
elif start < 0 and s.stop is None:
elif start < 0 and s.stop is None and step > 0:
obj = self.obj.tail(abs(start))
return obj if (step == 1) else obj.gather_every(step)

Expand Down
22 changes: 16 additions & 6 deletions py-polars/tests/parametric/test_lazyframe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ----------------------------------------------------
# Validate LazyFrame behaviour with parametric tests
# ----------------------------------------------------
from hypothesis import given
from hypothesis import example, given
from hypothesis.strategies import integers

import polars as pl
Expand All @@ -16,25 +16,35 @@
column(
"start",
dtype=pl.Int8,
null_probability=0.15,
strategy=integers(min_value=-4, max_value=6),
null_probability=0.3,
strategy=integers(min_value=-3, max_value=4),
),
column(
"stop",
dtype=pl.Int8,
null_probability=0.15,
strategy=integers(min_value=-2, max_value=8),
null_probability=0.3,
strategy=integers(min_value=-2, max_value=6),
),
column(
"step",
dtype=pl.Int8,
null_probability=0.15,
null_probability=0.3,
strategy=integers(min_value=-3, max_value=3).filter(lambda x: x != 0),
),
column("misc", dtype=pl.Int32),
],
)
)
@example(
alexander-beedie marked this conversation as resolved.
Show resolved Hide resolved
ldf=pl.LazyFrame(
{
"start": [-1, None, 1, None, 1, -1],
"stop": [None, 0, -1, -1, 2, 1],
"step": [-1, -1, 1, None, -1, 1],
"misc": [1, 2, 3, 4, 5, 6],
}
)
)
def test_lazyframe_slice(ldf: pl.LazyFrame) -> None:
py_data = ldf.collect().rows()

Expand Down
18 changes: 18 additions & 0 deletions py-polars/tests/unit/operations/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,21 @@ def test_slice_pushdown_literal_projection_14349() -> None:
plan = q.explain()
assert plan.index("WITH_COLUMNS") < plan.index("SLICE")
assert q.collect().height == 0


@pytest.mark.parametrize(
"input_slice",
[
(-1, None, -1),
(None, 0, -1),
(1, -1, 1),
(None, -1, None),
(1, 2, -1),
(-1, 1, 1),
],
)
def test_slice_lazy_frame_raises_proper(input_slice: tuple[int | None]) -> None:
ldf = pl.LazyFrame({"a": [1, 2, 3]})
s = slice(*input_slice)
with pytest.raises(ValueError, match="not supported"):
ldf[s].collect()
Loading