Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/docs/source/src/python/user-guide/lazy/schema.py
7890 views
1
# --8<-- [start:setup]
2
import polars as pl
3
4
# --8<-- [end:setup]
5
6
# --8<-- [start:schema]
7
lf = pl.LazyFrame({"foo": ["a", "b", "c"], "bar": [0, 1, 2]})
8
9
print(lf.collect_schema())
10
# --8<-- [end:schema]
11
12
# --8<-- [start:lazyround]
13
lf = pl.LazyFrame({"foo": ["a", "b", "c"]}).with_columns(pl.col("foo").round(2))
14
# --8<-- [end:lazyround]
15
16
# --8<-- [start:typecheck]
17
try:
18
print(lf.collect())
19
except Exception as e:
20
print(f"{type(e).__name__}: {e}")
21
# --8<-- [end:typecheck]
22
23
# --8<-- [start:lazyeager]
24
lazy_eager_query = (
25
pl.LazyFrame(
26
{
27
"id": ["a", "b", "c"],
28
"month": ["jan", "feb", "mar"],
29
"values": [0, 1, 2],
30
}
31
)
32
.with_columns((2 * pl.col("values")).alias("double_values"))
33
.collect()
34
.pivot(index="id", on="month", values="double_values", aggregate_function="first")
35
.lazy()
36
.filter(pl.col("mar").is_null())
37
.collect()
38
)
39
print(lazy_eager_query)
40
# --8<-- [end:lazyeager]
41
42