{"record":{"id":"49b1a19a13cdc593","repo":"pandas-dev/pandas","slug":"unable-to-avoid-copy-while-creating-an-array-as-re-49b1a1","errorCode":null,"errorMessage":"Unable to avoid copy while creating an array as requested.","messagePattern":"Unable to avoid copy while creating an array as requested\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/interval.py","lineNumber":1568,"sourceCode":"        # non-strict inequality when closed != 'both'; at least one side is\n        # not included in the intervals, so equality does not imply overlapping\n        return bool(\n            (self._right[:-1] <= self._left[1:]).all()\n            or (self._left[:-1] >= self._right[1:]).all()\n        )\n\n    # ---------------------------------------------------------------------\n    # Conversion\n\n    def __array__(\n        self, dtype: NpDtype | None = None, copy: bool | None = None\n    ) -> np.ndarray:\n        \"\"\"\n        Return the IntervalArray's data as a numpy array of Interval\n        objects (with dtype='object')\n        \"\"\"\n        if copy is False:\n            raise ValueError(\n                \"Unable to avoid copy while creating an array as requested.\"\n            )\n\n        left = self._left\n        right = self._right\n        mask = self.isna()\n        closed = self.closed\n\n        result = np.empty(len(left), dtype=object)\n        for i, left_value in enumerate(left):\n            if mask[i]:\n                result[i] = np.nan\n            else:\n                result[i] = Interval(left_value, right[i], closed)\n        return result\n\n    def __arrow_array__(self, type=None):\n        \"\"\"","sourceCodeStart":1550,"sourceCodeEnd":1586,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/interval.py#L1550-L1586","documentation":"Raised by IntervalArray.__array__ when called with copy=False. The numpy-compatible conversion must build a fresh object array of Interval/NA values, which is inherently a copy; the protocol therefore refuses the no-copy contract rather than silently violating it. Triggered by np.asarray(arr, copy=False) or any code path that requests a zero-copy numpy view.","triggerScenarios":"Calling np.asarray(interval_array) under NumPy versions that pass copy=False, or library code (e.g. some sklearn/dask paths) explicitly requesting copy=False via __array__.","commonSituations":"NumPy 2.0 changed __array__ signature to add copy=None|True|False; downstream libs that pass copy=False now hit this. Upgrading NumPy without pinning compatible libs.","solutions":["Let pandas convert: call arr.to_numpy() (defaults to copy=True) or np.asarray(arr) without copy=False.","If a downstream library passes copy=False, upgrade it or call np.asarray(arr, dtype=object) explicitly.","Convert to object dtype ahead of time with arr.astype(object)."],"exampleFix":"# before\nnp.asarray(interval_arr, copy=False)\n\n# after\nnp.asarray(interval_arr, dtype=object)","handlingStrategy":"fallback","validationCode":"def to_numpy_object(arr):\n    return np.asarray(arr, dtype=object)","typeGuard":null,"tryCatchPattern":"try:\n    return np.asarray(arr, copy=False)\nexcept ValueError as e:\n    if 'avoid copy' in str(e):\n        return np.asarray(arr, dtype=object)","preventionTips":["Avoid passing copy=False to np.asarray on ExtensionArray subclasses.","Use arr.to_numpy() for the supported conversion path.","Pin or upgrade NumPy and downstream libs together to keep __array__ contract aligned."],"tags":["interval-array","numpy","copy"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}