pandas-dev/pandas · error · TypeError

Invalid value '{value!s}' for dtype '{self.dtype}'

Error message

Invalid value '{value!s}' for dtype '{self.dtype}'

What it means

Raised by BaseMaskedArray._validate_setitem_value when a scalar cannot be losslessly stored in the array's dtype. The method short-circuits to a TypeError when the value's kind is incompatible: e.g. a string into Int64, a float-with-fraction into Int64, a non-bool into Boolean, or a NaN where not allowed. The check protects the underlying numpy buffer from silent truncation.

Source

Thrown at pandas/core/arrays/masked.py:420

        TypeError
        """
        kind = self.dtype.kind
        # TODO: get this all from np_can_hold_element?
        if kind == "b":
            if lib.is_bool(value):
                return value

        elif kind == "f":
            if lib.is_integer(value) or lib.is_float(value):
                return value

        elif lib.is_integer(value) or (lib.is_float(value) and value.is_integer()):
            return value
            # TODO: unsigned checks

        # Note: without the "str" here, the f-string rendering raises in
        #  py38 builds.
        raise TypeError(f"Invalid value '{value!s}' for dtype '{self.dtype}'")

    def insert(self, loc: int, item) -> Self:
        if not is_valid_na_for_dtype(item, self.dtype):
            self._validate_setitem_value(item)
        return super().insert(loc, item)

    def _validate_listlike(self, value) -> tuple[np.ndarray, npt.NDArray[np.bool_]]:
        """
        Validate a non-scalar setitem value and return ``(data, mask)``.

        Raises
        ------
        TypeError
            If `value` cannot be losslessly stored in self.dtype.
        """
        kind = self.dtype.kind

        if hasattr(value, "dtype"):

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Cast the value before assigning: int(value), float(value), bool(value) as appropriate to arr.dtype.kind.
  2. Use pd.NA for missing values instead of 'nan' strings or None-with-type-mismatch.
  3. If you need heterogeneous values, switch the column dtype to object or string.

Example fix

# before
arr = pd.array([1, 2, 3], dtype='Int64')
arr[0] = '5'

# after
arr[0] = int('5')
Defensive patterns

Strategy: type-guard

Validate before calling

def coerce_scalar(arr, value):
    kind = arr.dtype.kind
    if kind == 'b':
        return bool(value)
    if kind in 'iu':
        return int(value)
    if kind == 'f':
        return float(value)
    return value

Type guard

def scalar_matches_dtype(arr, value) -> bool:
    kind = arr.dtype.kind
    if kind == 'b':
        return isinstance(value, bool)
    if kind in 'iu':
        return isinstance(value, int) and not isinstance(value, bool)
    if kind == 'f':
        return isinstance(value, (int, float)) and not isinstance(value, bool)
    return False

Try / catch

try:
    arr[i] = value
except TypeError as e:
    if 'Invalid value' in str(e):
        arr[i] = coerce_scalar(arr, value)

Prevention

When it happens

Trigger: Setting arr[i] = 'x' on an Int64 array, arr[i] = 1.5 on an Int64 array, arr[i] = 1 on a Boolean array, or any scalar whose kind doesn't match the masked array's dtype.kind.

Common situations: User input parsed as strings reaching numeric columns, mixed-type CSV data, conditional assignments where the value's type wasn't coerced.

Related errors


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