Skip to content

Commit

Permalink
test(python): Reorganize tests for clear operation (#15304)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Mar 26, 2024
1 parent a661965 commit 2964d5e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 42 deletions.
4 changes: 0 additions & 4 deletions py-polars/tests/unit/constructors/test_constructors.py
Expand Up @@ -167,10 +167,6 @@ def test_init_dict() -> None:
)
assert df.schema == {"c": pl.Int8, "d": pl.Int16}

dfe = df.clear()
assert df.schema == dfe.schema
assert len(dfe) == 0

# empty nested objects
for empty_val in [None, "", {}, []]: # type: ignore[var-annotated]
test = [{"field": {"sub_field": empty_val, "sub_field_2": 2}}]
Expand Down
13 changes: 0 additions & 13 deletions py-polars/tests/unit/dataframe/test_df.py
Expand Up @@ -1701,19 +1701,6 @@ def test_df_schema_unique() -> None:
df.rename({"b": "a"})


def test_cleared() -> None:
df = pl.DataFrame(
{"a": [1, 2], "b": [True, False]}, schema_overrides={"a": pl.UInt32}
)
dfc = df.clear()
assert dfc.schema == df.schema
assert dfc.rows() == []

dfc = df.clear(3)
assert dfc.schema == df.schema
assert dfc.rows() == [(None, None), (None, None), (None, None)]


def test_empty_projection() -> None:
empty_df = pl.DataFrame({"a": [1, 2], "b": [3, 4]}).select([])
assert empty_df.rows() == []
Expand Down
76 changes: 76 additions & 0 deletions py-polars/tests/unit/operations/test_clear.py
@@ -0,0 +1,76 @@
from __future__ import annotations

import pytest
from hypothesis import given
from hypothesis.strategies import integers

import polars as pl
from polars.testing.parametric import series


@given(s=series(), n=integers(min_value=0, max_value=10))
def test_clear_series_parametric(s: pl.Series, n: int) -> None:
result = s.clear()

assert result.dtype == s.dtype
assert result.name == s.name
assert result.is_empty()

result = s.clear(n)

assert result.dtype == s.dtype
assert result.name == s.name
assert len(result) == n
assert result.null_count() == n


@pytest.mark.parametrize("n", [0, 2, 5])
def test_clear_series(n: int) -> None:
a = pl.Series(name="a", values=[1, 2, 3], dtype=pl.Int16)

result = a.clear(n)
assert result.dtype == a.dtype
assert result.name == a.name
assert len(result) == n
assert result.null_count() == n


def test_clear_df() -> None:
df = pl.DataFrame(
{"a": [1, 2], "b": [True, False]}, schema={"a": pl.UInt32, "b": pl.Boolean}
)

result = df.clear()
assert result.schema == df.schema
assert result.rows() == []

result = df.clear(3)
assert result.schema == df.schema
assert result.rows() == [(None, None), (None, None), (None, None)]


def test_clear_lf() -> None:
lf = pl.LazyFrame(
{
"foo": [1, 2, 3],
"bar": [6.0, 7.0, 8.0],
"ham": ["a", "b", "c"],
}
)
ldfe = lf.clear()
assert ldfe.schema == lf.schema

ldfe = lf.clear(2)
assert ldfe.schema == lf.schema
assert ldfe.collect().rows() == [(None, None, None), (None, None, None)]


@pytest.mark.skip("Currently bugged: https://github.com/pola-rs/polars/issues/15303")
def test_clear_series_object_starting_with_null() -> None:
s = pl.Series([None, object()])

result = s.clear()

assert result.dtype == s.dtype
assert result.name == s.name
assert result.is_empty()
9 changes: 1 addition & 8 deletions py-polars/tests/unit/series/test_series.py
Expand Up @@ -1089,15 +1089,8 @@ def test_empty() -> None:
pl.Series(dtype=pl.Int32), pl.Series(dtype=pl.Int64), check_dtype=False
)

a = pl.Series(name="a", values=[1, 2, 3], dtype=pl.Int16)
for n in (0, 2, 5):
empty_a = a.clear(n)
assert a.dtype == empty_a.dtype
assert a.name == empty_a.name
assert len(empty_a) == n

with pytest.raises(TypeError, match="ambiguous"):
not empty_a
not pl.Series()


def test_round() -> None:
Expand Down
20 changes: 3 additions & 17 deletions py-polars/tests/unit/test_lazy.py
Expand Up @@ -1110,30 +1110,16 @@ def test_quantile_filtered_agg() -> None:


def test_lazy_schema() -> None:
ldf = pl.LazyFrame(
{
"foo": [1, 2, 3],
"bar": [6.0, 7.0, 8.0],
"ham": ["a", "b", "c"],
}
)
assert ldf.schema == {"foo": pl.Int64, "bar": pl.Float64, "ham": pl.String}

ldf = pl.LazyFrame(
lf = pl.LazyFrame(
{
"foo": [1, 2, 3],
"bar": [6.0, 7.0, 8.0],
"ham": ["a", "b", "c"],
}
)
assert ldf.dtypes == [pl.Int64, pl.Float64, pl.String]

ldfe = ldf.clear()
assert ldfe.schema == ldf.schema
assert lf.schema == {"foo": pl.Int64, "bar": pl.Float64, "ham": pl.String}

ldfe = ldf.clear(2)
assert ldfe.schema == ldf.schema
assert ldfe.collect().rows() == [(None, None, None), (None, None, None)]
assert lf.dtypes == [pl.Int64, pl.Float64, pl.String]


def test_predicate_count_vstack() -> None:
Expand Down

0 comments on commit 2964d5e

Please sign in to comment.