Skip to content

yggdrasil.pandas.tests

tests

Unittest base class for pandas tests.

Quick start

::

from yggdrasil.testing import PandasTestCase

class TestMyTransform(PandasTestCase):
    def test_groupby_sum(self):
        df = self.df({"k": ["a", "a", "b"], "v": [1, 2, 3]})
        result = df.groupby("k", as_index=False)["v"].sum()
        self.assertFrameEqual(result, {"k": ["a", "b"], "v": [3, 3]})

Auto-install

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

PandasTestCase

Bases: TestCase

Base class for pandas integration tests.

Attributes

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

df

df(data: Any, **kwargs: Any) -> 'pd.DataFrame'

Shorthand for pd.DataFrame(data, **kwargs).

series

series(data: Any, **kwargs: Any) -> 'pd.Series'

Shorthand for pd.Series(data, **kwargs).

arrow_to_pandas

arrow_to_pandas(table: 'pa.Table') -> 'pd.DataFrame'

Convert a pa.Table to a pandas DataFrame.

pandas_to_arrow

pandas_to_arrow(df: 'pd.DataFrame') -> 'pa.Table'

Convert a pandas DataFrame to a pa.Table.

Loads pyarrow on demand via runtime_import_module; will raise :class:unittest.SkipTest if pyarrow isn't available.

assertFrameEqual

assertFrameEqual(
    actual: "pd.DataFrame",
    expected: "pd.DataFrame | dict[str, Any] | list[dict[str, Any]]",
    *,
    ordered: bool = True,
    check_dtype: bool = True,
    check_index: bool = False,
    **kwargs: Any
) -> None

Assert two DataFrames are equal.

Parameters

actual : pd.DataFrame expected : pd.DataFrame | dict | list[dict] If a dict or list of dicts is given, it's coerced via pd.DataFrame. ordered : bool, default True If False, both sides are sorted by all columns before compare and the index is reset. check_dtype : bool, default True check_index : bool, default False If False, reset both indexes before comparing. kwargs : Additional args forwarded to pandas.testing.assert_frame_equal.

assertSeriesEqual

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

Assert two Series are equal.

check_names defaults to True when expected is a Series and False when it's a raw list (a list has no name).