Skip to content

Commit

Permalink
fix(python): Raise properly for slices not supported by LazyFrame (#…
Browse files Browse the repository at this point in the history
…15331)

Co-authored-by: alexander-beedie <alexander.m.beedie@icloud.com>
  • Loading branch information
petrosbar and alexander-beedie committed Mar 27, 2024
1 parent c6ba62c commit 39b486e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
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(
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()

0 comments on commit 39b486e

Please sign in to comment.