pandas-dev/pandas · error · ValueError

invalid dtype specified

Error message

invalid dtype specified {dtype}

What it means

Raised by NumericDtype._standardize_dtype when the input dtype string or np.dtype does not map to any registered NumericDtype in the dtype mapping. The mapping only covers supported numpy dtypes (e.g. int8/16/32/64, uint8/16/32/64, float32/64); anything else triggers a KeyError that is converted into this ValueError.

Solutions

  1. Use a supported nullable numeric dtype: 'Int8','Int16','Int32','Int64','UInt8'..'UInt64','Float32','Float64'.
  2. For complex/datetime/other data, choose the matching pandas dtype instead of a NumericDtype.
  3. Print np.dtype(dtype) to confirm the canonical numpy dtype before passing it in.

Example fix

// before
pd.array([1, 2, 3], dtype='Int128')   # raises
// after
pd.array([1, 2, 3], dtype='Int64')
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
from pandas.core.arrays.numeric import NumericDtype
mapping = NumericDtype._get_dtype_mapping()
if np.dtype(dtype) not in mapping:
    raise ValueError(f'Unsupported NumericDtype: {dtype}')

Type guard

def is_supported_numeric_dtype(dtype) -> bool:
    import numpy as np
    from pandas.core.arrays.numeric import NumericDtype
    return np.dtype(dtype) in NumericDtype._get_dtype_mapping()

Try / catch

try:
    arr = pd.array(values, dtype=dtype)
except ValueError as e:
    if 'invalid dtype specified' in str(e):
        arr = pd.array(values, dtype='Int64')
    else:
        raise

Prevention

When it happens

Trigger: Passing an unsupported dtype to a nullable numeric array constructor or astype, e.g. pd.array(values, dtype='Int128'), or a numpy dtype like np.dtype('complex128') / datetime64 to NumericDtype._standardize_dtype.

Common situations: Typos in dtype strings; requesting dtypes pandas nullable arrays do not support (Int128, complex, float16 on some paths); passing a numpy dtype object that does not round-trip to a registered NumericDtype.

Related errors


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

Appendix: source

Thrown at pandas/core/arrays/numeric.py:124

    def _get_dtype_mapping(cls) -> Mapping[np.dtype, NumericDtype]:
        raise AbstractMethodError(cls)

    @classmethod
    def _standardize_dtype(cls, dtype: NumericDtype | str | np.dtype) -> NumericDtype:
        """
        Convert a string representation or a numpy dtype to NumericDtype.
        """
        if isinstance(dtype, str) and (dtype.startswith(("Int", "UInt", "Float"))):
            # Avoid DeprecationWarning from NumPy about np.dtype("Int64")
            # https://github.com/numpy/numpy/pull/7476
            dtype = dtype.lower()

        if not isinstance(dtype, NumericDtype):
            mapping = cls._get_dtype_mapping()
            try:
                dtype = mapping[np.dtype(dtype)]
            except KeyError as err:
                raise ValueError(f"invalid dtype specified {dtype}") from err
        return dtype

    @classmethod
    def _safe_cast(cls, values: np.ndarray, dtype: np.dtype, copy: bool) -> np.ndarray:
        """
        Safely cast the values to the given dtype.

        "safe" in this context means the casting is lossless.
        """
        raise AbstractMethodError(cls)


def _coerce_to_data_and_mask(values, dtype, copy: bool, dtype_cls: type[NumericDtype]):
    checker = dtype_cls._checker
    default_dtype = dtype_cls._default_np_dtype

    mask = None
    inferred_type = None

View on GitHub (pinned to 3b7651241d)