Skip to content

yggdrasil.polars.tests

tests

Unittest base class for Polars tests.

Quick start

::

from yggdrasil.testing import PolarsTestCase
import polars as pl

class TestMyFilter(PolarsTestCase):
    def test_filter(self):
        df = self.df({"id": [1, 2, 3], "val": ["a", "b", "c"]})
        result = df.filter(pl.col("id") > 1)
        self.assertFrameEqual(result, {"id": [2, 3], "val": ["b", "c"]})

Auto-install

Uses :func:yggdrasil.environ.runtime_import_module to load polars. Set auto_install = True on the subclass or export YGG_TEST_AUTO_INSTALL=1 to install it automatically; otherwise a missing polars skips the class with an install hint.

PolarsTestCase

Bases: TestCase

Base class for Polars integration tests.

Attributes

pl : module The imported polars module. Populated by setUpClass. tmp_path : pathlib.Path Per-test scratch directory.

df

df(data: Any, schema: Any = None) -> 'pl.DataFrame'

Shorthand for pl.DataFrame(data, schema=schema).

lazy

lazy(data: Any, schema: Any = None) -> 'pl.LazyFrame'

Shorthand for pl.LazyFrame(data, schema=schema).

series

series(name: str, values: Any) -> 'pl.Series'

Shorthand for pl.Series(name, values).

arrow_to_polars

arrow_to_polars(table: 'pa.Table') -> 'pl.DataFrame'

Convert a pa.Table to a Polars DataFrame (zero-copy).

polars_to_arrow

polars_to_arrow(df: 'pl.DataFrame') -> 'pa.Table'

Convert a Polars DataFrame to a pa.Table (zero-copy).

write_parquet

write_parquet(df: 'pl.DataFrame', path: Path | str) -> Path

Write df to path as Parquet and return the path.

read_parquet

read_parquet(path: Path | str) -> 'pl.DataFrame'

Read a Parquet file as a Polars DataFrame.

assertFrameEqual

assertFrameEqual(
    actual: "pl.DataFrame | pl.LazyFrame",
    expected: "pl.DataFrame | pl.LazyFrame | dict[str, Any] | list[dict[str, Any]]",
    *,
    ordered: bool = True,
    check_dtypes: bool = True,
    check_column_order: bool = True,
    **kwargs: Any
) -> None

Assert two Polars frames are equal.

Parameters

actual : pl.DataFrame | pl.LazyFrame expected : pl.DataFrame | pl.LazyFrame | dict | list[dict] If a dict or list of dicts is given, it's coerced via pl.DataFrame. ordered : bool, default True If False, both sides are sorted by all columns before compare. check_dtypes : bool, default True check_column_order : bool, default True If False, columns are reordered to match expected. kwargs : Additional args forwarded to polars.testing.assert_frame_equal.

assertSeriesEqual

assertSeriesEqual(
    actual: "pl.Series",
    expected: "pl.Series | list[Any]",
    *,
    check_dtype: bool = True,
    **kwargs: Any
) -> None

Assert two Polars Series are equal.

assertSchemaEqual

assertSchemaEqual(
    actual: "pl.DataFrame | pl.LazyFrame",
    expected_fields: list[tuple[str, Any]],
) -> None

Assert a frame has exactly the given (name, dtype) fields.

dtype may be a Polars dtype class or its string name.