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_drop_nulls.py
8420 views
1
from __future__ import annotations
2
3
import pytest
4
from hypothesis import given
5
6
import polars as pl
7
from polars.testing import assert_frame_equal, assert_series_equal
8
from polars.testing.parametric import series
9
10
11
@given(s=series(allow_null=True))
12
def test_drop_nulls_parametric(s: pl.Series) -> None:
13
result = s.drop_nulls()
14
assert result.len() == s.len() - s.null_count()
15
16
filter_result = s.filter(s.is_not_null())
17
assert_series_equal(result, filter_result)
18
19
20
def test_df_drop_nulls_struct() -> None:
21
df = pl.DataFrame(
22
{"x": [{"a": 1, "b": 2}, {"a": 1, "b": None}, {"a": None, "b": 2}, None]}
23
)
24
25
result = df.drop_nulls()
26
27
expected = df.head(3)
28
assert_frame_equal(result, expected)
29
30
31
@pytest.mark.parametrize("maintain_order", [False, True])
32
def test_drop_nulls_in_agg_25349(maintain_order: bool) -> None:
33
lf = pl.LazyFrame({"a": [1, 2], "b": [None, 1]})
34
q = lf.group_by("a", maintain_order=maintain_order).agg(
35
pl.col.b.first().drop_nulls()
36
)
37
assert_frame_equal(
38
q.collect(),
39
pl.DataFrame({"a": [1, 2], "b": [[], [1]]}),
40
check_row_order=maintain_order,
41
)
42
43
44
@pytest.mark.parametrize("maintain_order", [False, True])
45
def test_drop_nulls_on_literal_25355(maintain_order: bool) -> None:
46
df = pl.DataFrame({"key": [0, 1]})
47
result = df.group_by("key", maintain_order=maintain_order).agg(
48
x=pl.lit(0, dtype=pl.Int64).drop_nulls()
49
)
50
assert_frame_equal(
51
result,
52
df.with_columns(x=pl.lit([0], dtype=pl.List(pl.Int64))),
53
check_row_order=maintain_order,
54
)
55
56