pandas-dev/pandas · error · TypeError
Invalid value ' ' for dtype
Error message
Invalid value '{value!s}' for dtype '{self.dtype}' What it means
Raised by BaseMaskedArray._validate_setitem_value when a scalar value cannot be losslessly stored in the array's dtype. Integer masked arrays accept integers or integer-valued floats, float arrays accept any integer or float, and boolean arrays accept only bools; anything else (strings, non-integer floats into Int dtypes, None) is rejected to avoid silent coercion.
Solutions
- Convert the value to the array's native type before assignment (int(value), float(value), bool(value)).
- Change the array's dtype to one that can hold the value, e.g. arr = arr.astype('Float64').
- Sanitize incoming data so only values valid for self.dtype reach the setitem path.
Example fix
// before
arr = pd.array([1, 2, 3], dtype='Int64')
arr[0] = 1.5 # raises: 1.5 is not integer-valued
// after
arr = arr.astype('Float64')
arr[0] = 1.5 Defensive patterns
Strategy: validation
Validate before calling
def coerce_to_dtype(dtype, value):
kind = dtype.kind
if kind == 'b':
return bool(value)
if kind == 'f':
return float(value)
if value is not None and (isinstance(value, float) and not float(value).is_integer()):
raise ValueError(f'{value!r} not losslessly storable in {dtype}')
return int(value)
arr[i] = coerce_to_dtype(arr.dtype, value) Type guard
def is_valid_for_dtype(dtype, value) -> bool:
import numpy as np
k = dtype.kind
if k == 'b':
return isinstance(value, (bool, np.bool_))
if k == 'f':
return isinstance(value, (int, float, np.integer, np.floating)) and not isinstance(value, bool)
if k in 'iu':
return isinstance(value, (int, np.integer)) or (isinstance(value, float) and value.is_integer())
return False Try / catch
try:
arr[i] = value
except TypeError as e:
if 'Invalid value' in str(e):
arr = arr.astype('Float64')
arr[i] = value
else:
raise Prevention
- Normalize incoming scalars to the column dtype before assignment.
- When upgrading an Int column to hold fractional values, astype('Float64') first.
- Validate user-supplied values against arr.dtype.kind in input layers.
When it happens
Trigger: Assigning arr[i] = value, arr.fillna(value), arr.insert(loc, item), or any setitem path where value is a scalar that fails the kind checks in _validate_setitem_value (e.g. arr[i] = 1.5 on an Int64 array, or arr[i] = 'x' on any numeric masked array).
Common situations: Mixing dtypes after refactoring a column from float to Int; loading CSV data typed as object then assigning string cells into a nullable Int column; passing user input without normalization; bugs where a column is typed Int64 but the upstream producer emits floats.
Related errors
- cannot convert to ' '-dtype NumPy array with missing…
- Expected array of type, got instead
- FloatingArray does not support np.float16 dtype.
- interpolate is not implemented for dtype=
- Invalid value for dtype 'str'. Value should be a string or…
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/16ec625874b9c560.
Report an issue: GitHub.
Appendix: 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 3b7651241d)