{"record":{"id":"152e9b641e635931","repo":"pandas-dev/pandas","slug":"cannot-modify-read-only-array-152e9b","errorCode":null,"errorMessage":"Cannot modify read-only array","messagePattern":"Cannot modify read-only array","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/interval.py","lineNumber":696,"sourceCode":"            # scalar\n            if is_scalar(left) and isna(left):\n                return self._fill_value\n            return Interval(left, right, self.closed)\n        if np.ndim(left) > 1:\n            # GH#30588 multi-dimensional indexer disallowed\n            raise ValueError(\"multi-dimensional indexing not allowed\")\n        # Argument 2 to \"_simple_new\" of \"IntervalArray\" has incompatible type\n        # \"Union[Period, Timestamp, Timedelta, NaTType, DatetimeArray, TimedeltaArray,\n        # ndarray[Any, Any]]\"; expected \"Union[Union[DatetimeArray, TimedeltaArray],\n        # ndarray[Any, Any]]\"\n        result = self._simple_new(left, right, dtype=self.dtype)  # type: ignore[arg-type]\n        if getitem_returns_view(self, key):\n            result._readonly = self._readonly\n        return result\n\n    def __setitem__(self, key, value) -> None:\n        if self._readonly:\n            raise ValueError(\"Cannot modify read-only array\")\n\n        key = check_array_indexer(self, key)\n        value_left, value_right = self._validate_setitem_value(value)\n\n        self._left[key] = value_left\n        self._right[key] = value_right\n\n    def _cmp_method(self, other, op):\n        # ensure pandas array for list-like and eliminate non-interval scalars\n        if is_list_like(other):\n            if not isinstance(\n                other, (list, np.ndarray, ExtensionArray)\n            ) and not ops.has_castable_attr(other):\n                warnings.warn(\n                    f\"Operation with {type(other).__name__} is deprecated. \"\n                    \"In a future version these will be treated as scalar-like. \"\n                    \"To retain the old behavior, explicitly wrap in a Series \"\n                    \"instead.\",","sourceCodeStart":678,"sourceCodeEnd":714,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/interval.py#L678-L714","documentation":"Raised by `IntervalArray.__setitem__` when the underlying array is marked read-only (`_readonly = True`). The flag is propagated from a parent numpy array that has `writeable=False`, preventing silent data corruption. Fires at pandas/core/arrays/interval.py:696.","triggerScenarios":"Calling `ia[i] = value` on an IntervalArray constructed from a read-only numpy buffer (e.g., shared memory, mmap, or `.flags.writeable=False`); or after slicing a parent that shares memory with a read-only source.","commonSituations":"Loading read-only memory-mapped arrays, parquet/arrow zero-copy buffers, or numpy arrays explicitly frozen for thread-safety.","solutions":["Copy before mutating: `ia = ia.copy()` then assign.","Make the underlying buffer writable: `arr.flags.writeable = True` if you own it.","Build the IntervalArray from a fresh `np.array(source)` instead of the read-only view."],"exampleFix":"// before\nia[0] = pd.Interval(1, 2)  # ia is read-only\n// after\nia = ia.copy()\nia[0] = pd.Interval(1, 2)","handlingStrategy":"validation","validationCode":"def writable_interval_array(ia):\n    if getattr(ia, '_readonly', False):\n        ia = ia.copy()\n    return ia","typeGuard":"def is_writable(ia) -> bool:\n    return not getattr(ia, '_readonly', False)","tryCatchPattern":"try:\n    ia[i] = value\nexcept ValueError as e:\n    if \"Cannot modify read-only\" in str(e):\n        ia = ia.copy()\n        ia[i] = value\n    else:\n        raise","preventionTips":["Copy arrays sourced from mmap/arrow/parquet before mutation.","Check `ia._readonly` (or arr.flags.writeable) before assigning.","Prefer building a new IntervalArray over in-place mutation when in doubt."],"tags":["interval","read-only","setitem","numpy"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}