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_clear.py
6939 views
1
from __future__ import annotations
2
3
import hypothesis.strategies as st
4
import pytest
5
from hypothesis import given
6
7
import polars as pl
8
from polars.testing.parametric import series
9
10
11
@given(s=series())
12
def test_clear_series_parametric(s: pl.Series) -> None:
13
result = s.clear()
14
15
assert result.dtype == s.dtype
16
assert result.name == s.name
17
assert result.is_empty()
18
19
20
@given(
21
s=series(
22
excluded_dtypes=[
23
pl.Struct, # See: https://github.com/pola-rs/polars/issues/3462
24
]
25
),
26
n=st.integers(min_value=0, max_value=10),
27
)
28
def test_clear_series_n_parametric(s: pl.Series, n: int) -> None:
29
result = s.clear(n)
30
31
assert result.dtype == s.dtype
32
assert result.name == s.name
33
assert len(result) == n
34
assert result.null_count() == n
35
36
37
@pytest.mark.parametrize("n", [0, 2, 5])
38
def test_clear_series(n: int) -> None:
39
a = pl.Series(name="a", values=[1, 2, 3], dtype=pl.Int16)
40
41
result = a.clear(n)
42
assert result.dtype == a.dtype
43
assert result.name == a.name
44
assert len(result) == n
45
assert result.null_count() == n
46
47
48
def test_clear_df() -> None:
49
df = pl.DataFrame(
50
{"a": [1, 2], "b": [True, False]}, schema={"a": pl.UInt32, "b": pl.Boolean}
51
)
52
53
result = df.clear()
54
assert result.schema == df.schema
55
assert result.rows() == []
56
57
result = df.clear(3)
58
assert result.schema == df.schema
59
assert result.rows() == [(None, None), (None, None), (None, None)]
60
61
62
def test_clear_lf() -> None:
63
lf = pl.LazyFrame(
64
{
65
"foo": [1, 2, 3],
66
"bar": [6.0, 7.0, 8.0],
67
"ham": ["a", "b", "c"],
68
}
69
)
70
ldfe = lf.clear()
71
assert ldfe.collect_schema() == lf.collect_schema()
72
73
ldfe = lf.clear(2)
74
assert ldfe.collect_schema() == lf.collect_schema()
75
assert ldfe.collect().rows() == [(None, None, None), (None, None, None)]
76
77
78
def test_clear_series_object_starting_with_null() -> None:
79
s = pl.Series([None, object()])
80
81
result = s.clear()
82
83
assert result.dtype == s.dtype
84
assert result.name == s.name
85
assert result.is_empty()
86
87
88
def test_clear_raise_negative_n() -> None:
89
s = pl.Series([1, 2, 3])
90
91
msg = "`n` should be greater than or equal to 0, got -1"
92
with pytest.raises(ValueError, match=msg):
93
s.clear(-1)
94
with pytest.raises(ValueError, match=msg):
95
s.to_frame().clear(-1)
96
97