polars.testing.assert_frame_equal#

polars.testing.assert_frame_equal(
left: DataFrame | LazyFrame,
right: DataFrame | LazyFrame,
*,
check_row_order: bool = True,
check_column_order: bool = True,
check_dtypes: bool = True,
check_exact: bool = False,
rtol: float = 1e-05,
atol: float = 1e-08,
categorical_as_str: bool = False,
) None[source]#

Assert that the left and right frame are equal.

Raises a detailed AssertionError if the frames differ. This function is intended for use in unit tests.

Parameters:
left

The first DataFrame or LazyFrame to compare.

right

The second DataFrame or LazyFrame to compare.

check_row_order

Require row order to match.

Note

Setting this to False requires sorting the data, which will fail on frames that contain unsortable columns.

check_column_order

Require column order to match.

check_dtypes

Require data types to match.

check_exact

Require float values to match exactly. If set to False, values are considered equal when within tolerance of each other (see rtol and atol). Only affects columns with a Float data type.

rtol

Relative tolerance for inexact checking. Fraction of values in right.

atol

Absolute tolerance for inexact checking.

categorical_as_str

Cast categorical columns to string before comparing. Enabling this helps compare columns that do not share the same string cache.

Notes

When using pytest, it may be worthwhile to shorten Python traceback printing by passing --tb=short. The default mode tends to be unhelpfully verbose. More information in the pytest docs.

Examples

>>> from polars.testing import assert_frame_equal
>>> df1 = pl.DataFrame({"a": [1, 2, 3]})
>>> df2 = pl.DataFrame({"a": [1, 5, 3]})
>>> assert_frame_equal(df1, df2)  
Traceback (most recent call last):
...
AssertionError: Series are different (value mismatch)
[left]:  [1, 2, 3]
[right]: [1, 5, 3]

The above exception was the direct cause of the following exception:

Traceback (most recent call last): … AssertionError: values for column ‘a’ are different