pandas-dev/pandas · error · AbstractMethodError

This method must be defined in the concrete class {name}

Error message

This method must be defined in the concrete class {name}

What it means

`AbstractMethodError` raised by the default `_validate_scalar` on `NDArrayBackedExtensionArray` (in `pandas/core/arrays/_mixins.py`). The message is the standard `AbstractMethodError` text ('This method must be defined in the concrete class {name}'). It signals that a concrete subclass forgot to override `_validate_scalar`, which pandas calls (e.g. via `NDArrayBackedExtensionIndex.insert`) to coerce an arbitrary Python value into the array's scalar type.

Source

Thrown at pandas/core/arrays/_mixins.py:114

    """

    _ndarray: np.ndarray

    # scalar used to denote NA value inside our self._ndarray, e.g. -1
    #  for Categorical, iNaT for Period. Outside of object dtype,
    #  self.isna() should be exactly locations in self._ndarray with
    #  _internal_fill_value.
    _internal_fill_value: Any

    def _box_func(self, x):
        """
        Wrap numpy type in our dtype.type if necessary.
        """
        return x

    def _validate_scalar(self, value):
        # used by NDArrayBackedExtensionIndex.insert
        raise AbstractMethodError(self)

    # ------------------------------------------------------------------------

    @overload
    def view(self, dtype: None = ...) -> Self: ...

    @overload
    def view(self, dtype: Dtype | None = ...) -> ArrayLike: ...

    def view(self, dtype: Dtype | None = None) -> ArrayLike:
        # We handle datetime64, datetime64tz, timedelta64, and period
        #  dtypes here. Everything else we pass through to the underlying
        #  ndarray.
        if dtype is None or dtype is self.dtype:
            return self._from_backing_data(self._ndarray)

        if isinstance(dtype, type):
            # we sometimes pass non-dtype objects, e.g np.ndarray;

View on GitHub (pinned to 3b7651241d)

Solutions

  1. Implement `_validate_scalar(self, value)` on the concrete subclass to convert/validate a scalar and return the dtype's native scalar (raise TypeError/ValueError on invalid input).
  2. Mirror an existing implementation, e.g. `pandas/core/arrays/integer.py` `_validate_scalar`, for the expected shape.
  3. If you did not mean to subclass, use a built-in dtype (IntegerArray, StringDtype, ArrowDtype) instead of a custom EA.

Example fix

// before
class MyArray(NDArrayBackedExtensionArray):
    _dtype = MyDtype()
    # _validate_scalar inherited -> AbstractMethodError on insert

// after
class MyArray(NDArrayBackedExtensionArray):
    _dtype = MyDtype()
    def _validate_scalar(self, value):
        if isinstance(value, self._dtype.type) or value is pd.NaT:
            return value
        raise TypeError(f'Invalid scalar {value!r} for {self.dtype}')
Defensive patterns

Strategy: type-guard

Validate before calling

import inspect
if not getattr(type(arr)._validate_scalar, '__isabstractmethod__', False) is False and 'AbstractMethodError' in inspect.getsource(type(arr)._validate_scalar):
    raise TypeError(f'{type(arr).__name__} does not implement _validate_scalar')

Type guard

def implements_validate_scalar(arr) -> bool:
    import inspect
    src = inspect.getsource(type(arr)._validate_scalar)
    return 'AbstractMethodError' not in src

Try / catch

from pandas.errors import AbstractMethodError
try:
    arr._validate_scalar(v)
except AbstractMethodError as e:
    raise NotImplementedError(f'Custom EA {type(arr).__name__} must implement _validate_scalar') from e

Prevention

When it happens

Trigger: Subclassing `NDArrayBackedExtensionArray` (or `ExtensionArray`) without implementing `_validate_scalar`, then performing an operation that needs scalar validation: `Index.insert`, `append` with a scalar, `fillna`, or `_validate_setitem_value` paths that delegate to it. Calling `arr._validate_scalar(value)` directly also triggers it.

Common situations: Writing a third-party ExtensionArray backed by a numpy ndarray and forgetting this hook; upgrading pandas and hitting a code path that newly calls `_validate_scalar`; copy-pasting an EA skeleton from an outdated tutorial.

Related errors


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