{"record":{"id":"62fdabce717dde6a","repo":"pandas-dev/pandas","slug":"left-base-r-is-right-base-r","errorCode":null,"errorMessage":"{left_base!r} is {right_base!r}","messagePattern":"\\{left_base!r\\} is \\{right_base!r\\}","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"pandas/_testing/asserters.py","lineNumber":793,"sourceCode":"\n    # instance validation\n    # Show a detailed error message when classes are different\n    assert_class_equal(left, right, obj=class_obj or obj)\n    # both classes must be an np.ndarray\n    _check_isinstance(left, right, np.ndarray)\n\n    def _get_base(obj: np.ndarray) -> Any:\n        return obj.base if getattr(obj, \"base\", None) is not None else obj\n\n    left_base = _get_base(left)\n    right_base = _get_base(right)\n\n    if check_same == \"same\":\n        if left_base is not right_base:\n            raise AssertionError(f\"{left_base!r} is not {right_base!r}\")\n    elif check_same == \"copy\":\n        if left_base is right_base:\n            raise AssertionError(f\"{left_base!r} is {right_base!r}\")\n\n    def _raise(left: np.ndarray, right: np.ndarray, err_msg: str | None) -> NoReturn:\n        if err_msg is None:\n            if left.shape != right.shape:\n                raise_assert_detail(\n                    obj, f\"{obj} shapes are different\", left.shape, right.shape\n                )\n\n            diff = 0.0\n            for left_arr, right_arr in zip(left, right, strict=True):\n                # count up differences\n                if not array_equivalent(left_arr, right_arr, strict_nan=strict_nan):\n                    diff += 1\n\n            diff = diff * 100.0 / left.size\n            msg = f\"{obj} values are different ({np.round(diff, 5)} %)\"\n            raise_assert_detail(obj, msg, left, right, index_values=index_values)\n","sourceCodeStart":775,"sourceCodeEnd":811,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/_testing/asserters.py#L775-L811","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["If you want value equality, drop check_same entirely.","If you genuinely want a copy, construct right as right = left.copy() so the bases differ.","Verify with 'left.base is right.base' in a REPL before asserting."],"exampleFix":"# before\nassert_numpy_array_equal(arr, arr.view(), check_same='copy')\n\n# after\nassert_numpy_array_equal(arr, arr.copy(), check_same='copy')","handlingStrategy":"validation","validationCode":"def distinct_base(a, b):\n    la = a.base if getattr(a, 'base', None) is not None else a\n    lb = b.base if getattr(b, 'base', None) is not None else b\n    return la is not lb\n# only call check_same='copy' when distinct_base(left, right) is True","typeGuard":"import numpy as np\n\ndef has_distinct_base(a: np.ndarray, b: np.ndarray) -> bool:\n    la = a.base if getattr(a, 'base', None) is not None else a\n    lb = b.base if getattr(b, 'base', None) is not None else b\n    return la is not lb","tryCatchPattern":null,"preventionTips":["Build the copy with .copy() before asserting check_same='copy'.","Reserve check_same for copy/view unit tests, not value comparisons.","Check the .base chain manually first."],"tags":["testing","assertion","numpy","memory","copy","assertionerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}