Path: blob/main/py-polars/tests/unit/operations/test_clear.py
6939 views
from __future__ import annotations12import hypothesis.strategies as st3import pytest4from hypothesis import given56import polars as pl7from polars.testing.parametric import series8910@given(s=series())11def test_clear_series_parametric(s: pl.Series) -> None:12result = s.clear()1314assert result.dtype == s.dtype15assert result.name == s.name16assert result.is_empty()171819@given(20s=series(21excluded_dtypes=[22pl.Struct, # See: https://github.com/pola-rs/polars/issues/346223]24),25n=st.integers(min_value=0, max_value=10),26)27def test_clear_series_n_parametric(s: pl.Series, n: int) -> None:28result = s.clear(n)2930assert result.dtype == s.dtype31assert result.name == s.name32assert len(result) == n33assert result.null_count() == n343536@pytest.mark.parametrize("n", [0, 2, 5])37def test_clear_series(n: int) -> None:38a = pl.Series(name="a", values=[1, 2, 3], dtype=pl.Int16)3940result = a.clear(n)41assert result.dtype == a.dtype42assert result.name == a.name43assert len(result) == n44assert result.null_count() == n454647def test_clear_df() -> None:48df = pl.DataFrame(49{"a": [1, 2], "b": [True, False]}, schema={"a": pl.UInt32, "b": pl.Boolean}50)5152result = df.clear()53assert result.schema == df.schema54assert result.rows() == []5556result = df.clear(3)57assert result.schema == df.schema58assert result.rows() == [(None, None), (None, None), (None, None)]596061def test_clear_lf() -> None:62lf = pl.LazyFrame(63{64"foo": [1, 2, 3],65"bar": [6.0, 7.0, 8.0],66"ham": ["a", "b", "c"],67}68)69ldfe = lf.clear()70assert ldfe.collect_schema() == lf.collect_schema()7172ldfe = lf.clear(2)73assert ldfe.collect_schema() == lf.collect_schema()74assert ldfe.collect().rows() == [(None, None, None), (None, None, None)]757677def test_clear_series_object_starting_with_null() -> None:78s = pl.Series([None, object()])7980result = s.clear()8182assert result.dtype == s.dtype83assert result.name == s.name84assert result.is_empty()858687def test_clear_raise_negative_n() -> None:88s = pl.Series([1, 2, 3])8990msg = "`n` should be greater than or equal to 0, got -1"91with pytest.raises(ValueError, match=msg):92s.clear(-1)93with pytest.raises(ValueError, match=msg):94s.to_frame().clear(-1)959697