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