pola-rs/polars · error · AssertionError
are different ( )\n[left]: \n[right]
Error message
{objects} are different ({detail})\n[left]: {left}\n[right]: {right} What it means
Shared helper that formats and raises the standard polars testing 'X are different (detail)' AssertionError with left/right values. It is the single raise site behind assert_series_equal / assert_frame_equal mismatch details.
Solutions
- Read the (detail) field: 'dtype mismatch', 'string_value mismatch', 'shape mismatch', etc., and fix the flagged aspect.
- For float data, pass check_exact=False with an appropriate abs_tol/rel_tol.
- Align dtypes explicitly with .cast() before comparing if the difference is benign.
- Set check_names=False when intentionally comparing series with different names.
Example fix
# before assert_series_equal(pl.Series([0.1 + 0.2]), pl.Series([0.3])) # fails # after assert_series_equal(pl.Series([0.1 + 0.2]), pl.Series([0.3]), check_exact=False, abs_tol=1e-9)
Defensive patterns
Strategy: validation
Validate before calling
if s1.dtype != s2.dtype:
s2 = s2.cast(s1.dtype)
assert_series_equal(s1, s2) Try / catch
try:
assert_series_equal(left, right)
except AssertionError as e:
print(e) # detail names the mismatched aspect
raise Prevention
- Read the (detail) line of the assertion message first
- Use check_exact=False with tolerances for floats
- Cast dtypes explicitly before comparisons to survive polars upgrades
When it happens
Trigger: Any assert_series_equal/assert_frame_equal call where values, dtypes, names, shapes, or null counts differ; _assert_correct_input_type when a wrong input type is passed to a testing helper.
Common situations: Unit tests comparing floats with NaN without check_nan; dtype drift after polars upgrade (Int32 vs Int64); column name mismatches (check_names); null vs NaN comparisons.
Related errors
- are equal (but are expected not to be)
- Series are equal (but are expected not to be)
- Cannot compare two series of different lengths.
- comparing categoricals with different Categories is not…
- comparing datetimes with different units or timezones is…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/4597bebf2d49d587.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/testing/asserts/utils.py:12
from __future__ import annotations
from typing import Any, NoReturn
def raise_assertion_error(
objects: str, detail: str, left: Any, right: Any, *, cause: Exception | None = None
) -> NoReturn:
"""Raise a detailed assertion error."""
__tracebackhide__ = True
msg = f"{objects} are different ({detail})\n[left]: {left}\n[right]: {right}"
raise AssertionError(msg) from cause
View on GitHub (pinned to fe841f959e)