pandas-dev/pandas · warning · null

_from_scalars should only raise ValueError or TypeError. Con

Error message

_from_scalars should only raise ValueError or TypeError. Consider overriding _from_scalars where appropriate.

What it means

Emitted as a UserWarning by ExtensionArray._from_scalars when the underlying _from_sequence raises an exception that is neither ValueError nor TypeError. The base implementation wraps _from_sequence and re-raises, warning first because _from_scalars is a strict contract: subclass authors are expected to override it and raise only ValueError/TypeError so _cast_pointwise_result can fall back cleanly.

Source

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

        scalars : sequence
        dtype : ExtensionDtype

        Raises
        ------
        TypeError or ValueError

        Notes
        -----
        This is called in a try/except block when casting the result of a
        pointwise operation in ExtensionArray._cast_pointwise_result.
        """
        try:
            return cls._from_sequence(scalars, dtype=dtype, copy=False)
        except (ValueError, TypeError):
            raise
        except Exception:
            warnings.warn(
                "_from_scalars should only raise ValueError or TypeError. "
                "Consider overriding _from_scalars where appropriate.",
                stacklevel=find_stack_level(),
            )
            raise

    def _cast_pointwise_result(self, values) -> ArrayLike:
        """
        Construct an ExtensionArray after a pointwise operation.

        Cast the result of a pointwise operation (e.g. Series.map) to an
        array. This is not required to return an ExtensionArray of the same
        type as self or of the same dtype. It can also return another
        ExtensionArray of the same "family" if you implement multiple
        ExtensionArrays/Dtypes that are interoperable (e.g. if you have float
        array with units, this method can return an int array with units).

        If converting to your own ExtensionArray is not possible, this method
        falls back to returning an array with the default type inference.

View on GitHub (pinned to 3b7651241d)

Solutions

  1. Override _from_scalars in your ExtensionArray subclass to validate scalars and raise only ValueError or TypeError.
  2. Inspect the chained exception (the warning re-raises the original) to find which _from_sequence branch raised the non-conforming error and convert it.
  3. Update/upgrade the third-party extension array package; newer versions typically override _from_scalars.
  4. Filter the warning only as a last resort (warnings.filterwarnings) while reporting the upstream bug.

Example fix

// before
class MyArray(ExtensionArray):
    @classmethod
    def _from_sequence(cls, scalars, dtype=None, copy=False):
        raise KeyError('bad scalar')  # non-conforming -> warning

// after
class MyArray(ExtensionArray):
    @classmethod
    def _from_scalars(cls, scalars, *, dtype):
        try:
            return cls._from_sequence(scalars, dtype=dtype, copy=False)
        except KeyError as err:
            raise ValueError(str(err)) from err
Defensive patterns

Strategy: try-catch

Validate before calling

import warnings
from pandas.core.arrays.base import ExtensionArray

def safe_from_scalars(cls, scalars, dtype):
    with warnings.catch_warnings():
        warnings.simplefilter('error', UserWarning)
        try:
            return cls._from_scalars(scalars, dtype=dtype)
        except (ValueError, TypeError):
            return None  # let _cast_pointwise_result fall back

Type guard

def from_scalars_conformant(cls) -> bool:
    # base _from_scalars wraps _from_sequence; a conformant subclass overrides it
    return '_from_scalars' in cls.__dict__

Try / catch

import warnings
with warnings.catch_warnings(record=True) as caught:
    try:
        result = arr._cast_pointwise_result(values)
    except Exception:
        result = None  # fall back to default type inference
for w in caught:
    if '_from_scalars' in str(w.message):
        # report upstream: subclass must override _from_scalars
        ...

Prevention

When it happens

Trigger: Writing a custom ExtensionArray whose _from_sequence raises a non-ValueError/TypeError (e.g. NotImplementedError, KeyError, AssertionError) when given pointwise-operation scalars; running Series.map / elementwise ops that route through _cast_pointwise_result on such an array.

Common situations: Third-party/pandas-2 extension array implementations that haven't overridden _from_scalars; dtype coercion paths during groupby/apply/map that pass unexpected scalar shapes; hitting an internal assert inside _from_sequence during a pointwise cast.

Related errors


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