pandas-dev/pandas · error · AssertionError

is not

Error message

{left_base!r} is not {right_base!r}

What it means

Raised inside `assert_numpy_array_equal` when `check_same='same'` is set and the two arrays do NOT share the same memory base. This flag verifies memory-sharing semantics (whether two arrays are views of the same buffer), not value equality. The message prints the `.base` of each side to show which buffer each points to.

Solutions

  1. If a copy is now expected, change `check_same='same'` to `check_same='copy'`.
  2. If a view is required, restructure the producing code to slice rather than copy (e.g. `df['x'].values` vs `df['x'].to_numpy()`).
  3. For value-only tests, omit `check_same` entirely.

Example fix

// before
assert_numpy_array_equal(a, b, check_same='same')

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

Strategy: validation

Validate before calling

def shares_base(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 gb
assert shares_base(left, right)

Prevention

When it happens

Trigger: Calling `assert_numpy_array_equal(a, b, check_same='same')` where `b` is a copy of `a` rather than a view; testing that an operation returned a view when it actually returned new memory (e.g. after a dtype change or a reset_index).

Common situations: Internal pandas development tests verifying copy/view semantics; refactors that change whether an op returns a view; copy-on-write changes that introduce defensive copies.

Related errors


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

Appendix: source

Thrown at pandas/_testing/asserters.py:790

        appropriate assertion message.
    """
    __tracebackhide__ = True

    # 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

View on GitHub (pinned to 3b7651241d)