pandas-dev/pandas · error · AssertionError

is

Error message

{left_base!r} is {right_base!r}

What it means

Raised inside `assert_numpy_array_equal` when `check_same='copy'` is set and the two arrays DO share the same memory base. This asserts that an operation produced an independent copy when the test expected one. The message prints both `.base` values to show they alias the same buffer.

Solutions

  1. If a view is now expected, switch to `check_same='same'`.
  2. If a true copy is required, force one at the producing site (`np.ascontiguousarray(x)` or `x.copy()`).
  3. Remove `check_same` for pure value-equality tests.

Example fix

// before
b = a[:]
assert_numpy_array_equal(a, b, check_same='copy')

// after
b = a.copy()
assert_numpy_array_equal(a, b, check_same='copy')
Defensive patterns

Strategy: validation

Validate before calling

def are_independent(a, b) -> bool:
    import numpy as np
    ga = a.base if getattr(a, 'base', None) is not None else a
    gb = b.base if getattr(b, 'base', None) is not None else b
    return ga is not gb
assert are_independent(left, right)

Prevention

When it happens

Trigger: Calling `assert_numpy_array_equal(a, b, check_same='copy')` where `b = a` or `b = a[:]` (a view, not a copy); pandas-internal tests that confirm CoW produced a defensive copy finding that it instead returned the original buffer.

Common situations: Copy-on-write regressions; refactors to indexing code; numpy version changes that alter slicing semantics.

Related errors


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

Appendix: source

Thrown at pandas/_testing/asserters.py:793

    # instance validation
    # Show a detailed error message when classes are different
    assert_class_equal(left, right, obj=class_obj or obj)
    # both classes must be an np.ndarray
    _check_isinstance(left, right, np.ndarray)

    def _get_base(obj: np.ndarray) -> Any:
        return obj.base if getattr(obj, "base", None) is not None else obj

    left_base = _get_base(left)
    right_base = _get_base(right)

    if check_same == "same":
        if left_base is not right_base:
            raise AssertionError(f"{left_base!r} is not {right_base!r}")
    elif check_same == "copy":
        if left_base is right_base:
            raise AssertionError(f"{left_base!r} is {right_base!r}")

    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)

View on GitHub (pinned to 3b7651241d)