{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/base.py#L553-L589","documentation":"Raised (NotImplementedError) from the base ExtensionArray.__setitem__ when the array is not read-only but the subclass has not overridden `__setitem__`. Per the docstring, __setitem__ is not strictly required by the ExtensionArray interface, so the base class only raises this if a mutation is actually attempted on a non-readonly subclass that omitted it. The message names the offending type.","triggerScenarios":"Calling `arr[i] = value` or `arr[mask] = value` on an ExtensionArray subclass that intentionally (or accidentally) does not implement __setitem__, e.g. an immutable or read-mostly custom array.","commonSituations":"Building a custom immutable extension array and forgetting to either mark it read-only or make __setitem__ raise a clearer error; mutating an array type whose author considered assignment unsupported.","solutions":["If the type should be mutable, implement `__setitem__(self, key, value)` in the subclass (set self._readonly=False accordingly).","If the type is immutable by design, mark buffers read-only so the clearer 'Cannot modify read-only array' path triggers, or override __setitem__ to raise a domain-specific TypeError."],"exampleFix":"# before\nclass MyArray(ExtensionArray):\n    ...   # no __setitem__\n# after\nclass MyArray(ExtensionArray):\n    def __setitem__(self, key, value):\n        self._data[key] = value","handlingStrategy":"validation","validationCode":"if '__setitem__' not in vars(type(arr)):\n    raise TypeError(f\"{type(arr).__name__} is immutable: no __setitem__\")\narr[0] = value","typeGuard":"def is_mutable(arr) -> bool:\n    return '__setitem__' in vars(type(arr))","tryCatchPattern":"try:\n    arr[0] = value\nexcept NotImplementedError as e:\n    if \"does not implement __setitem__\" in str(e):\n        raise TypeError(f\"{type(arr).__name__} is immutable by design\")\n    raise","preventionTips":["Implement __setitem__ on mutable ExtensionArray subclasses","Mark truly-immutable arrays read-only for a clearer error"],"tags":["extension-array","setitem","not-implemented","subclassing"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}