Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/lazyframe/test_getitem.py
6939 views
1
# ----------------------------------------------------
2
# Validate LazyFrame behaviour with parametric tests
3
# ----------------------------------------------------
4
import hypothesis.strategies as st
5
from hypothesis import example, given
6
7
import polars as pl
8
from polars.testing.parametric import column, dataframes
9
10
11
@given(
12
ldf=dataframes(
13
max_size=10,
14
lazy=True,
15
cols=[
16
column(
17
"start",
18
dtype=pl.Int8,
19
allow_null=True,
20
strategy=st.integers(min_value=-3, max_value=4),
21
),
22
column(
23
"stop",
24
dtype=pl.Int8,
25
allow_null=True,
26
strategy=st.integers(min_value=-2, max_value=6),
27
),
28
column(
29
"step",
30
dtype=pl.Int8,
31
allow_null=True,
32
strategy=st.integers(min_value=-3, max_value=3).filter(
33
lambda x: x != 0
34
),
35
),
36
column("misc", dtype=pl.Int32),
37
],
38
)
39
)
40
@example(
41
ldf=pl.LazyFrame(
42
{
43
"start": [-1, None, 1, None, 1, -1],
44
"stop": [None, 0, -1, -1, 2, 1],
45
"step": [-1, -1, 1, None, -1, 1],
46
"misc": [1, 2, 3, 4, 5, 6],
47
}
48
)
49
)
50
def test_lazyframe_getitem(ldf: pl.LazyFrame) -> None:
51
py_data = ldf.collect().rows()
52
53
for start, stop, step, _ in py_data:
54
s = slice(start, stop, step)
55
sliced_py_data = py_data[s]
56
try:
57
sliced_df_data = ldf[s].collect().rows()
58
assert sliced_py_data == sliced_df_data, (
59
f"slice [{start}:{stop}:{step}] failed on lazy df w/len={len(py_data)}"
60
)
61
62
except ValueError as exc:
63
# test params will trigger some known
64
# unsupported cases; filter them here.
65
if "not supported" not in str(exc):
66
raise
67
68