{"record":{"id":"63f6255712af0015","repo":"pandas-dev/pandas","slug":"cannot-modify-read-only-array","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/_mixins.py","lineNumber":243,"sourceCode":"        # override base class by adding axis keyword\n        validate_bool_kwarg(skipna, \"skipna\")\n        if not skipna and self._hasna:\n            raise ValueError(\"Encountered an NA value with skipna=False\")\n        return nargminmax(self, \"argmax\", axis=axis)\n\n    def unique(self) -> Self:\n        new_data = unique(self._ndarray)\n        return self._from_backing_data(new_data)\n\n    def sort(\n        self,\n        *,\n        ascending: bool = True,\n        kind: SortKind = \"quicksort\",\n        na_position: str = \"last\",\n    ) -> None:\n        if self._readonly:\n            raise ValueError(\"Cannot modify read-only array\")\n        sort_indices = self.argsort(\n            ascending=ascending, kind=kind, na_position=na_position\n        )\n        self._ndarray[:] = self._ndarray[sort_indices]\n\n    @classmethod\n    def _concat_same_type(\n        cls,\n        to_concat: Sequence[Self],\n        axis: AxisInt = 0,\n    ) -> Self:\n        \"\"\"\n        Concatenate multiple arrays of this dtype.\n\n        Parameters\n        ----------\n        to_concat : sequence of this type\n","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/_mixins.py#L225-L261","documentation":"NDArrayBackedExtensionArray.sort sorts in place by writing reordered values back into the backing ndarray via self._ndarray[:] = .... If the backing buffer is read-only (the _readonly flag is set, propagated from views of read-only sources), the in-place write cannot succeed and pandas raises ValueError before attempting it.","triggerScenarios":"Calling arr.sort() on a read-only extension array obtained from a numpy array with WRITEABLE=False, a memory-mapped buffer, np.frombuffer, or a view propagated from a read-only parent.","commonSituations":"Data loaded read-only from disk/mmap, or arrays derived from .values round-trips; expecting sort to return a copy.","solutions":["Sort out-of-place: use arr.argsort() with take, or np.sort(arr) to obtain a sorted copy.","Copy first to get a writable buffer: arr = arr.copy(); arr.sort().","Avoid relying on in-place sort on slices/views of read-only data."],"exampleFix":"// before\narr.sort()\n// after\narr = arr.copy()\narr.sort()","handlingStrategy":"validation","validationCode":"def safe_sort(arr):\n    if getattr(arr, \"_readonly\", False):\n        arr = arr.copy()\n    arr.sort()\n    return arr","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Copy read-only arrays before in-place ops","Prefer argsort()+take or np.sort() over in-place sort on views"],"tags":["read-only","in-place","memory","mutation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}