pandas-dev/pandas · error · AssertionError

{err_msg}

Error message

{err_msg}

What it means

Raised by `assert_numpy_array_equal` when the caller supplies a custom `err_msg` and the arrays turn out not to be equal. When `err_msg` is provided it bypasses pandas' detailed diff formatting and the caller's message is re-raised verbatim. When `err_msg` is None pandas instead builds a richer message with shape and percentage-different detail (other branches).

Source

Thrown at pandas/_testing/asserters.py:812

    def _raise(left: np.ndarray, right: np.ndarray, err_msg: str | None) -> NoReturn:
        if err_msg is None:
            if left.shape != right.shape:
                raise_assert_detail(
                    obj, f"{obj} shapes are different", left.shape, right.shape
                )

            diff = 0.0
            for left_arr, right_arr in zip(left, right, strict=True):
                # count up differences
                if not array_equivalent(left_arr, right_arr, strict_nan=strict_nan):
                    diff += 1

            diff = diff * 100.0 / left.size
            msg = f"{obj} values are different ({np.round(diff, 5)} %)"
            raise_assert_detail(obj, msg, left, right, index_values=index_values)

        raise AssertionError(err_msg)

    # compare shape and values
    if not array_equivalent(left, right, strict_nan=strict_nan):
        _raise(left, right, err_msg)

    if check_dtype:
        if isinstance(left, np.ndarray) and isinstance(right, np.ndarray):
            assert_attr_equal("dtype", left, right, obj=obj)


@set_module("pandas.testing")
def assert_extension_array_equal(
    left: ExtensionArray,
    right: ExtensionArray,
    check_dtype: bool | Literal["equiv"] = True,
    index_values: Index | np.ndarray | None = None,
    check_exact: bool | lib.NoDefault = lib.no_default,
    rtol: float | lib.NoDefault = lib.no_default,

View on GitHub (pinned to 3b7651241d)

Solutions

  1. Set `err_msg=None` temporarily to see pandas' detailed diff and pinpoint the mismatch.
  2. Inspect the values of `left` and `right` directly before the assert call.
  3. If shape differs, fix the producing code so both sides have matching dimensions.

Example fix

// before
assert_numpy_array_equal(left, right, err_msg='arrays differ')

// after
assert_numpy_array_equal(left, right)  # shows detailed diff
Defensive patterns

Strategy: try-catch

Try / catch

try:
    assert_numpy_array_equal(left, right)  # err_msg=None
except AssertionError:
    # your custom diagnostic with full context
    raise AssertionError(f'pipeline stage X produced mismatch: {left} vs {right}')

Prevention

When it happens

Trigger: Calling `assert_numpy_array_equal(left, right, err_msg='my custom diagnostic')` where left and right differ in value or shape; internal pandas asserts that pipe a context message through `err_msg`.

Common situations: Test suites that add custom diagnostics to numpy comparisons; running an older test where the underlying data changed.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/bcfed529da17683a. Report an issue: GitHub.