pandas-dev/pandas · error · AssertionError

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

Error message

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

What it means

Inside assert_numpy_array_equal (asserters.py:788-790), when check_same='same' the function asserts that left and right numpy arrays share the same underlying base memory (i.e. one is a view/alias of the other). If left_base is not right_base it raises AssertionError("{left_base!r} is not {right_base!r}"). This is an internal testing utility used to verify memory-sharing behavior, not value equality.

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 71959b8cb9)

Solutions

  1. If you want value equality, remove check_same (or set it to None) and rely on the default value comparison.
  2. If you genuinely want same-memory, ensure right = left or right = left.view(...) so they share base memory.
  3. For copy-vs-view tests, build the right operand as right = left to satisfy check_same='same'.

Example fix

# before
assert_numpy_array_equal(arr, arr.copy(), check_same='same')

# after
assert_numpy_array_equal(arr, arr, check_same='same')
Defensive patterns

Strategy: validation

Validate before calling

def same_base(a, b):
    la = a.base if getattr(a, 'base', None) is not None else a
    lb = b.base if getattr(b, 'base', None) is not None else b
    return la is lb
# only call check_same='same' when same_base(left, right) is True

Type guard

import numpy as np

def shares_base(a: np.ndarray, b: np.ndarray) -> bool:
    la = a.base if getattr(a, 'base', None) is not None else a
    lb = b.base if getattr(b, 'base', None) is not None else b
    return la is lb

Prevention

When it happens

Trigger: Calling assert_numpy_array_equal(a, b, check_same='same') where a and b are distinct arrays (a copy was made, or they were constructed independently). The check looks at the .base attribute (numpy's view-chain root) to decide if they alias the same buffer.

Common situations: Pandas-internal tests asserting that an operation returns a view rather than a copy; accidentally passing check_same='same' when you meant to compare values; a refactoring that introduced a copy where a view was expected.

Related errors


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