Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/operations/arithmetic/utils.py
6940 views
1
from __future__ import annotations
2
3
from typing import Any
4
5
import polars as pl
6
7
8
def exec_op_with_series(lhs: pl.Series, rhs: pl.Series, op: Any) -> pl.Series:
9
v: pl.Series = op(lhs, rhs)
10
return v
11
12
13
def exec_op_with_expr(lhs: pl.Series, rhs: pl.Series, op: Any) -> pl.Series:
14
return pl.select(lhs).lazy().select(op(pl.first(), rhs)).collect().to_series()
15
16
17
def exec_op_with_expr_no_type_coercion(
18
lhs: pl.Series, rhs: pl.Series, op: Any
19
) -> pl.Series:
20
optimizations = pl.QueryOptFlags()
21
optimizations._pyoptflags.type_coercion = False
22
return (
23
pl.select(lhs)
24
.lazy()
25
.select(op(pl.first(), rhs))
26
.collect(optimizations=optimizations)
27
.to_series()
28
)
29
30
31
BROADCAST_LEN = 3
32
33
34
def broadcast_left(
35
l: pl.Series, # noqa: E741
36
r: pl.Series,
37
o: pl.Series,
38
) -> tuple[pl.Series, pl.Series, pl.Series]:
39
return l.new_from_index(0, BROADCAST_LEN), r, o.new_from_index(0, BROADCAST_LEN)
40
41
42
def broadcast_right(
43
l: pl.Series, # noqa: E741
44
r: pl.Series,
45
o: pl.Series,
46
) -> tuple[pl.Series, pl.Series, pl.Series]:
47
return l, r.new_from_index(0, BROADCAST_LEN), o.new_from_index(0, BROADCAST_LEN)
48
49
50
def broadcast_both(
51
l: pl.Series, # noqa: E741
52
r: pl.Series,
53
o: pl.Series,
54
) -> tuple[pl.Series, pl.Series, pl.Series]:
55
return (
56
l.new_from_index(0, BROADCAST_LEN),
57
r.new_from_index(0, BROADCAST_LEN),
58
o.new_from_index(0, BROADCAST_LEN),
59
)
60
61
62
def broadcast_none(
63
l: pl.Series, # noqa: E741
64
r: pl.Series,
65
o: pl.Series,
66
) -> tuple[pl.Series, pl.Series, pl.Series]:
67
return l, r, o
68
69
70
BROADCAST_SERIES_COMBINATIONS = [
71
broadcast_left,
72
broadcast_right,
73
broadcast_both,
74
broadcast_none,
75
]
76
77
EXEC_OP_COMBINATIONS = [
78
exec_op_with_series,
79
exec_op_with_expr,
80
exec_op_with_expr_no_type_coercion,
81
]
82
83