pandas-dev/pandas · error · TypeError
cannot be converted to
Error message
{values.dtype} cannot be converted to {name} What it means
Raised by _coerce_to_data_and_mask when the input values have object/string dtype, lib.infer_dtype reports 'boolean', and no explicit target dtype was provided. Constructing a nullable numeric array from an object array of booleans without a dtype is ambiguous (boolean is not numeric), so pandas refuses rather than guessing.
Solutions
- Pass an explicit numeric dtype: pd.array(values, dtype='Int64').
- Convert the booleans to integers first: np.asarray(values, dtype=int).
- If booleans are intended, use dtype='boolean' instead of a numeric NumericArray.
Example fix
// before _coerce_to_data_and_mask(np.array([True, False], dtype=object), None, ...) # raises // after _coerce_to_data_and_mask(np.array([True, False], dtype=object), 'Int64', ...)
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
from pandas.core.lib import infer_dtype
if values.dtype == object and infer_dtype(values, skipna=True) == 'boolean' and dtype is None:
dtype = 'Int64' # or 'boolean', per intent Type guard
def object_array_is_safe_for_numeric(values, dtype) -> bool:
from pandas.core.lib import infer_dtype
return not (values.dtype == object and infer_dtype(values, skipna=True) == 'boolean' and dtype is None) Try / catch
try:
_coerce_to_data_and_mask(values, dtype, ...)
except TypeError as e:
if 'cannot be converted' in str(e):
_coerce_to_data_and_mask(values, 'Int64', ...)
else:
raise Prevention
- Always pass an explicit dtype when constructing nullable numeric arrays from object data.
- Convert object boolean arrays to int before passing to numeric constructors.
- Run infer_dtype on object columns during ingestion to plan dtype choices.
When it happens
Trigger: Calling _coerce_to_data_and_mask on an object-dtype ndarray (or list-like inferred as object) whose contents are all booleans, with dtype=None; equivalent to pd.array([True, False, True]) without specifying a numeric dtype.
Common situations: Building a nullable Int/Float array from a column that was stored as object dtype booleans; reading mixed data that inferred as boolean; forgetting to pass dtype when the source is a Python list of bools routed through object.
Related errors
- Expected array of type, got instead
- interpolate is not implemented for dtype=
- invalid dtype specified
- ArrowStringArray requires a PyArrow (chunked) array of…
- bad operand type for unary +
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/4e80a43ac99dedcc.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/numeric.py:173
values = values.astype(dtype.numpy_dtype, copy=False)
if copy:
values = values.copy()
mask = mask.copy()
return values, mask
original = values
if not copy:
values = np.asarray(values)
else:
values = np.array(values, copy=copy)
inferred_type = None
if values.dtype == object or is_string_dtype(values.dtype):
inferred_type = lib.infer_dtype(values, skipna=True)
if inferred_type == "boolean" and dtype is None:
# object dtype array of bools
name = dtype_cls.__name__.strip("_")
raise TypeError(f"{values.dtype} cannot be converted to {name}")
elif values.dtype.kind == "b" and checker(dtype):
# fastpath
mask = np.zeros(len(values), dtype=np.bool_)
if not copy:
values = np.asarray(values, dtype=default_dtype)
else:
values = np.array(values, dtype=default_dtype, copy=copy)
elif values.dtype.kind not in "iuf":
name = dtype_cls.__name__.strip("_")
raise TypeError(f"{values.dtype} cannot be converted to {name}")
if values.ndim != 1:
raise TypeError("values must be a 1D list-like")
if mask is None:
if values.dtype.kind in "iu":View on GitHub (pinned to 3b7651241d)