pandas-dev/pandas · error · NotImplementedError

{type(self)} does not implement __setitem__.

Error message

{type(self)} does not implement __setitem__.

What it means

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.

Source

Thrown at pandas/core/arrays/base.py:571

        #
        # * Setting multiple values : ExtensionArrays should support setting
        #   multiple values at once, 'key' will be a sequence of integers and
        #  'value' will be a same-length sequence.
        #
        # * Broadcasting : For a sequence 'key' and a scalar 'value',
        #   each position in 'key' should be set to 'value'.
        #
        # * Coercion : Most users will expect basic coercion to work. For
        #   example, a string like '2018-01-01' is coerced to a datetime
        #   when setting on a datetime64ns array. In general, if the
        #   __init__ method coerces that value, then so should __setitem__
        # Note, also, that Series/DataFrame.where internally use __setitem__
        # on a copy of the data.
        # Check if the array is readonly
        if self._readonly:
            raise ValueError("Cannot modify read-only array")

        raise NotImplementedError(f"{type(self)} does not implement __setitem__.")

    def __len__(self) -> int:
        """
        Length of this array

        Returns
        -------
        length : int
        """
        raise AbstractMethodError(self)

    def __iter__(self) -> Iterator[Any]:
        """
        Iterate over elements of the array.
        """
        # This needs to be implemented so that pandas recognizes extension
        # arrays as list-like. The default implementation makes successive
        # calls to ``__getitem__``, which may be slower than necessary.

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. If you own the subclass, implement `__setitem__(self, key, value)` to mutate the backing storage.
  2. If you are a consumer, create a new array with the desired change instead of mutating: rebuild via constructor or use pd.Series.replace.
  3. Convert to a backed array that supports setitem: `s.astype(object)` or to a numpy array.
  4. File an issue with the third-party array library to implement __setitem__.

Example fix

# before (custom ExtensionArray subclass missing __setitem__)
class MyArray(ExtensionArray): ...
arr = MyArray(...)
arr[0] = 5  # NotImplementedError: <class 'MyArray'> does not implement __setitem__

# after: implement __setitem__ in the subclass
def __setitem__(self, key, value):
    # validate key/value, mutate self._data accordingly
    ...
Defensive patterns

Strategy: type-guard

Validate before calling

def safe_setitem(arr, key, value):
    import inspect
    cls_setitem = type(arr).__setitem__
    if cls_setitem is ExtensionArray.__setitem__:
        raise NotImplementedError(f"{type(arr).__name__} does not implement __setitem__; rebuild the array instead")
    arr[key] = value

Type guard

from pandas.core.arrays.base import ExtensionArray

def supports_setitem(arr) -> bool:
    return type(arr).__setitem__ is not ExtensionArray.__setitem__

Try / catch

try:
    arr[key] = value
except NotImplementedError as e:
    if "does not implement __setitem__" in str(e):
        # rebuild via constructor instead of mutating
        arr = type(arr)._from_sequence([...updated values...])
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/7d07686f4655ca5e. Report an issue: GitHub.