{"record":{"id":"7d07686f4655ca5e","repo":"pandas-dev/pandas","slug":"type-self-does-not-implement-setitem","errorCode":null,"errorMessage":"{type(self)} does not implement __setitem__.","messagePattern":"(.+?) does not implement __setitem__\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/base.py","lineNumber":571,"sourceCode":"        #\n        # * Setting multiple values : ExtensionArrays should support setting\n        #   multiple values at once, 'key' will be a sequence of integers and\n        #  'value' will be a same-length sequence.\n        #\n        # * Broadcasting : For a sequence 'key' and a scalar 'value',\n        #   each position in 'key' should be set to 'value'.\n        #\n        # * Coercion : Most users will expect basic coercion to work. For\n        #   example, a string like '2018-01-01' is coerced to a datetime\n        #   when setting on a datetime64ns array. In general, if the\n        #   __init__ method coerces that value, then so should __setitem__\n        # Note, also, that Series/DataFrame.where internally use __setitem__\n        # on a copy of the data.\n        # Check if the array is readonly\n        if self._readonly:\n            raise ValueError(\"Cannot modify read-only array\")\n\n        raise NotImplementedError(f\"{type(self)} does not implement __setitem__.\")\n\n    def __len__(self) -> int:\n        \"\"\"\n        Length of this array\n\n        Returns\n        -------\n        length : int\n        \"\"\"\n        raise AbstractMethodError(self)\n\n    def __iter__(self) -> Iterator[Any]:\n        \"\"\"\n        Iterate over elements of the array.\n        \"\"\"\n        # This needs to be implemented so that pandas recognizes extension\n        # arrays as list-like. The default implementation makes successive\n        # calls to ``__getitem__``, which may be slower than necessary.","sourceCodeStart":553,"sourceCodeEnd":589,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/base.py#L553-L589","documentation":"Raised by the base ExtensionArray.__setitem__ fallback for extension array subclasses that do not override __setitem__. After the readonly check passes, if the subclass never implemented item assignment, pandas raises NotImplementedError naming the concrete class. This is a 'subclass incomplete' signal rather than a runtime data condition.","triggerScenarios":"Calling `arr[i] = v` on an instance of an ExtensionArray subclass whose author did not implement __setitem__. Custom/third-party ExtensionArray subclasses are the usual culprits; first-class pandas arrays generally override it.","commonSituations":"Writing a custom ExtensionArray and forgetting __setitem__; using a third-party array class with incomplete setitem support; attempting in-place edits on read-only-style custom arrays.","solutions":["If you own the subclass, implement `__setitem__(self, key, value)` to mutate the backing storage.","If you are a consumer, create a new array with the desired change instead of mutating: rebuild via constructor or use pd.Series.replace.","Convert to a backed array that supports setitem: `s.astype(object)` or to a numpy array.","File an issue with the third-party array library to implement __setitem__."],"exampleFix":"# before (custom ExtensionArray subclass missing __setitem__)\nclass MyArray(ExtensionArray): ...\narr = MyArray(...)\narr[0] = 5  # NotImplementedError: <class 'MyArray'> does not implement __setitem__\n\n# after: implement __setitem__ in the subclass\ndef __setitem__(self, key, value):\n    # validate key/value, mutate self._data accordingly\n    ...","handlingStrategy":"type-guard","validationCode":"def safe_setitem(arr, key, value):\n    import inspect\n    cls_setitem = type(arr).__setitem__\n    if cls_setitem is ExtensionArray.__setitem__:\n        raise NotImplementedError(f\"{type(arr).__name__} does not implement __setitem__; rebuild the array instead\")\n    arr[key] = value","typeGuard":"from pandas.core.arrays.base import ExtensionArray\n\ndef supports_setitem(arr) -> bool:\n    return type(arr).__setitem__ is not ExtensionArray.__setitem__","tryCatchPattern":"try:\n    arr[key] = value\nexcept NotImplementedError as e:\n    if \"does not implement __setitem__\" in str(e):\n        # rebuild via constructor instead of mutating\n        arr = type(arr)._from_sequence([...updated values...])\n    else:\n        raise","preventionTips":["Implement __setitem__ in custom ExtensionArray subclasses.","Avoid in-place mutation of third-party/extension arrays; rebuild them.","Unit-test custom ExtensionArrays for setitem support."],"tags":["extension-array","subclass","not-implemented","mutation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}