Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/operations/test_has_nulls.py
6939 views
1
from hypothesis import given
2
3
import polars as pl
4
from polars.testing import assert_frame_equal
5
from polars.testing.parametric import dataframes, series
6
7
8
@given(s=series(allow_null=False))
9
def test_has_nulls_series_no_nulls(s: pl.Series) -> None:
10
assert s.has_nulls() is False
11
12
13
@given(df=dataframes(allow_null=False))
14
def test_has_nulls_expr_no_nulls(df: pl.DataFrame) -> None:
15
result = df.select(pl.all().has_nulls())
16
assert result.select(pl.any_horizontal(df.columns)).item() is False
17
18
19
@given(
20
s=series(
21
excluded_dtypes=[
22
pl.Struct, # https://github.com/pola-rs/polars/issues/3462
23
]
24
)
25
)
26
def test_has_nulls_series_parametric(s: pl.Series) -> None:
27
result = s.has_nulls()
28
assert result == (s.null_count() > 0)
29
assert result == s.is_null().any()
30
31
32
@given(
33
lf=dataframes(
34
excluded_dtypes=[
35
pl.Struct, # https://github.com/pola-rs/polars/issues/3462
36
],
37
lazy=True,
38
)
39
)
40
def test_has_nulls_expr_parametric(lf: pl.LazyFrame) -> None:
41
result = lf.select(pl.all().has_nulls())
42
43
assert_frame_equal(result, lf.select(pl.all().null_count() > 0))
44
assert_frame_equal(result, lf.select(pl.all().is_null().any()))
45
46
47
def test_has_nulls_series() -> None:
48
s = pl.Series([1, 2, None])
49
assert s.has_nulls() is True
50
assert s[:2].has_nulls() is False
51
52
53
def test_has_nulls_expr() -> None:
54
lf = pl.LazyFrame({"a": [1, 2, None], "b": ["x", "y", "z"]})
55
result = lf.select(pl.all().has_nulls())
56
expected = pl.LazyFrame({"a": [True], "b": [False]})
57
assert_frame_equal(result, expected)
58
59