pandas-dev/pandas · error · NotImplementedError

does not implement __setitem__.

Error message

{type(self)} does not implement __setitem__.

What it means

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.

Solutions

  1. If the type should be mutable, implement `__setitem__(self, key, value)` in the subclass (set self._readonly=False accordingly).
  2. 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.

Example fix

# before
class MyArray(ExtensionArray):
    ...   # no __setitem__
# after
class MyArray(ExtensionArray):
    def __setitem__(self, key, value):
        self._data[key] = value
Defensive patterns

Strategy: validation

Validate before calling

if '__setitem__' not in vars(type(arr)):
    raise TypeError(f"{type(arr).__name__} is immutable: no __setitem__")
arr[0] = value

Type guard

def is_mutable(arr) -> bool:
    return '__setitem__' in vars(type(arr))

Try / catch

try:
    arr[0] = value
except NotImplementedError as e:
    if "does not implement __setitem__" in str(e):
        raise TypeError(f"{type(arr).__name__} is immutable by design")
    raise

Prevention

When it happens

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

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

Related errors


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

Appendix: 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 3b7651241d)