Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/expr/test_udfs.py
6939 views
1
from typing import Any
2
3
import pytest
4
5
import polars as pl
6
7
8
def test_pass_name_alias_18914() -> None:
9
df = pl.DataFrame({"id": [1], "value": [2]})
10
11
assert df.select(
12
pl.all()
13
.map_elements(
14
lambda x: x,
15
skip_nulls=False,
16
pass_name=True,
17
return_dtype=pl.Int64,
18
)
19
.over("id")
20
).to_dict(as_series=False) == {"id": [1], "value": [2]}
21
22
23
@pytest.mark.parametrize(
24
"dtype",
25
[
26
pl.String,
27
pl.Int64,
28
pl.Boolean,
29
pl.List(pl.Int32),
30
pl.Array(pl.Boolean, 2),
31
pl.Struct({"a": pl.Int8}),
32
pl.Enum(["a"]),
33
],
34
)
35
def test_raises_udf(dtype: pl.DataType) -> None:
36
def raise_f(item: Any) -> None:
37
msg = "test error"
38
raise ValueError(msg)
39
40
with pytest.raises(ValueError, match="test error"):
41
pl.select(
42
pl.lit(1).map_elements(
43
raise_f,
44
return_dtype=dtype,
45
)
46
)
47
48