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_collect_schema.py
8446 views
1
import pytest
2
from hypothesis import given
3
4
import polars as pl
5
from polars.testing.parametric import dataframes
6
7
8
@given(lf=dataframes(lazy=True))
9
def test_collect_schema_parametric(lf: pl.LazyFrame) -> None:
10
assert lf.collect_schema() == lf.collect().schema
11
12
13
def test_collect_schema() -> None:
14
lf = pl.LazyFrame(
15
{
16
"foo": [1, 2, 3],
17
"bar": [6.0, 7.0, 8.0],
18
"ham": ["a", "b", "c"],
19
}
20
)
21
result = lf.collect_schema()
22
expected = pl.Schema({"foo": pl.Int64(), "bar": pl.Float64(), "ham": pl.String()})
23
assert result == expected
24
25
26
def test_collect_schema_with_row_index_duplicate() -> None:
27
lf = pl.LazyFrame({"index": []}).with_row_index()
28
with pytest.raises(
29
pl.exceptions.DuplicateError, match="duplicate column name index"
30
):
31
_ = lf.collect_schema()
32
33
lf = pl.LazyFrame({}).with_row_index().with_row_index()
34
with pytest.raises(
35
pl.exceptions.DuplicateError, match="duplicate column name index"
36
):
37
_ = lf.collect_schema()
38
39
40
def test_collect_schema_unpivot_duplicate() -> None:
41
lf = pl.LazyFrame({"variable": [], "a": []}).unpivot(["a"])
42
with pytest.raises(
43
pl.exceptions.DuplicateError, match="duplicate column name 'variable'"
44
):
45
_ = lf.collect_schema()
46
47
lf = pl.LazyFrame({"value": [], "a": []}).unpivot(["a"])
48
with pytest.raises(
49
pl.exceptions.DuplicateError, match="duplicate column name 'value'"
50
):
51
_ = lf.collect_schema()
52
53