pandas-dev/pandas · error · AssertionError

{left_base!r} is {right_base!r}

Error message

{left_base!r} is {right_base!r}

What it means

Inside assert_numpy_array_equal (asserters.py:791-793), when check_same='copy' the function asserts that left and right numpy arrays do NOT share base memory (i.e. right is an independent copy). If left_base is right_base it raises AssertionError("{left_base!r} is {right_base!r}"). This is the inverse of check_same='same' and is used to verify that a copy was actually produced.

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

Solutions

  1. If you want value equality, drop check_same entirely.
  2. If you genuinely want a copy, construct right as right = left.copy() so the bases differ.
  3. Verify with 'left.base is right.base' in a REPL before asserting.

Example fix

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

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

Strategy: validation

Validate before calling

def distinct_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 not lb
# only call check_same='copy' when distinct_base(left, right) is True

Type guard

import numpy as np

def has_distinct_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 not lb

Prevention

When it happens

Trigger: Calling assert_numpy_array_equal(a, b, check_same='copy') where b aliases a's buffer (e.g. b = a or b = a.view()) — the code expected a copy but got a view. The check compares the .base roots of the two arrays.

Common situations: Pandas-internal tests asserting an operation copies data for safety; an optimization that started returning views where a copy was previously guaranteed (Copy-on-Write changes); passing the same object as both arguments.

Related errors


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