Path: blob/main/py-polars/tests/unit/dataframe/test_equals.py
6939 views
import pytest12import polars as pl345def test_equals() -> None:6# Values are checked7df1 = pl.DataFrame(8{9"foo": [1, 2, 3],10"bar": [6.0, 7.0, 8.0],11"ham": ["a", "b", "c"],12}13)14df2 = pl.DataFrame(15{16"foo": [3, 2, 1],17"bar": [8.0, 7.0, 6.0],18"ham": ["c", "b", "a"],19}20)2122assert df1.equals(df1) is True23assert df1.equals(df2) is False2425# Column names are checked26df3 = pl.DataFrame(27{28"a": [1, 2, 3],29"b": [6.0, 7.0, 8.0],30"c": ["a", "b", "c"],31}32)33assert df1.equals(df3) is False3435# Datatypes are NOT checked36df = pl.DataFrame(37{38"foo": [1, 2, None],39"bar": [6.0, 7.0, None],40"ham": ["a", "b", None],41}42)43assert df.equals(df.with_columns(pl.col("foo").cast(pl.Int8))) is True44assert df.equals(df.with_columns(pl.col("ham").cast(pl.Categorical))) is True4546# The null_equal parameter determines if None values are considered equal47assert df.equals(df) is True48assert df.equals(df, null_equal=False) is False495051def test_equals_bad_input_type() -> None:52df1 = pl.DataFrame({"a": [1, 2, 3]})53df2 = pl.DataFrame({"a": [1, 2, 3]})5455with pytest.raises(56TypeError,57match="expected `other` .*to be a 'DataFrame'.* not 'LazyFrame'",58):59df1.equals(df2.lazy()) # type: ignore[arg-type]6061with pytest.raises(62TypeError,63match="expected `other` .*to be a 'DataFrame'.* not 'Series'",64):65df1.equals(pl.Series([1, 2, 3])) # type: ignore[arg-type]6667class DummyDataFrameSubclass(pl.DataFrame):68pass6970df2 = DummyDataFrameSubclass(df2)7172assert df1.equals(df2) is True737475