Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/test_scalar.py
6939 views
1
import pytest
2
3
import polars as pl
4
from polars.testing import assert_frame_equal
5
6
7
@pytest.mark.may_fail_auto_streaming
8
@pytest.mark.may_fail_cloud
9
def test_invalid_broadcast() -> None:
10
df = pl.DataFrame(
11
{
12
"a": [100, 103],
13
"group": [0, 1],
14
}
15
)
16
with pytest.raises(pl.exceptions.ShapeError):
17
df.select(pl.col("group").filter(pl.col("group") == 0), "a")
18
19
20
@pytest.mark.parametrize(
21
"dtype",
22
[
23
pl.Null,
24
pl.Int32,
25
pl.String,
26
pl.Enum(["foo"]),
27
pl.Binary,
28
pl.List(pl.Int32),
29
pl.Struct({"a": pl.Int32}),
30
pl.Array(pl.Int32, 1),
31
pl.List(pl.List(pl.Int32)),
32
],
33
)
34
def test_null_literals(dtype: pl.DataType) -> None:
35
assert (
36
pl.DataFrame([pl.Series("a", [1, 2], pl.Int64)])
37
.with_columns(pl.lit(None).cast(dtype).alias("b"))
38
.collect_schema()
39
.dtypes()
40
) == [pl.Int64, dtype]
41
42
43
def test_scalar_19957() -> None:
44
value = 1
45
values = [value] * 5
46
foo = pl.DataFrame({"foo": values})
47
foo_with_bar_from_literal = foo.with_columns(pl.lit(value).alias("bar"))
48
assert foo_with_bar_from_literal.gather_every(2).to_dict(as_series=False) == {
49
"foo": [1, 1, 1],
50
"bar": [1, 1, 1],
51
}
52
53
54
def test_scalar_len_20046() -> None:
55
df = pl.DataFrame({"a": [1, 2, 3]})
56
57
assert (
58
df.lazy()
59
.select(
60
pl.col("a"),
61
pl.lit(1),
62
)
63
.select(pl.len())
64
.collect()
65
.item()
66
== 3
67
)
68
69
q = pl.LazyFrame({"a": range(3)}).select(
70
pl.first("a"),
71
pl.col("a").alias("b"),
72
)
73
74
assert q.select(pl.len()).collect().item() == 3
75
76
77
def test_scalar_identification_function_expr_in_binary() -> None:
78
x = pl.Series("x", [1, 2, 3])
79
assert_frame_equal(
80
pl.select(x).with_columns(o=pl.col("x").null_count() > 0),
81
pl.select(x, o=False),
82
)
83
84
85
def test_scalar_rechunk_20627() -> None:
86
df = pl.concat(2 * [pl.Series([1])]).filter(pl.Series([False, True])).to_frame()
87
assert df.rechunk().to_series().n_chunks() == 1
88
89
90
def test_split_scalar_21581() -> None:
91
df = pl.DataFrame({"a": [1.0, 2.0, 3.0]})
92
df = df.with_columns(
93
[
94
pl.col("a").shift(-1).alias("next_a"),
95
pl.lit(True).alias("lit"),
96
]
97
)
98
99
assert df.filter(df["next_a"] != 99.0).with_columns(
100
[pl.lit(False).alias("lit")]
101
).to_dict(as_series=False) == {
102
"a": [1.0, 2.0],
103
"next_a": [2.0, 3.0],
104
"lit": [False, False],
105
}
106
107