pandas-dev/pandas · error · TypeError

{values.dtype} cannot be converted to {name}

Error message

{values.dtype} cannot be converted to {name}

What it means

Raised by _coerce_to_data_and_mask when the input is an object/string-dtype array whose inferred type is 'boolean' but no explicit dtype was requested. Constructing a nullable numeric array from a raw object array of booleans is ambiguous (use pd.array which routes to BooleanArray), so the numeric path refuses rather than silently coercing True/False into integers.

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 71959b8cb9)

Solutions

  1. Use pd.array(...) which infers the correct ExtensionArray (BooleanArray for bools).
  2. Explicitly cast the data first: np.asarray(values, dtype='int64') then construct Int64.
  3. Pass dtype='Int64' explicitly if you want bools coerced to 0/1.

Example fix

// before
arr = IntegerArray(np.array([True, False], dtype=object), ...)  # raises

// after
arr = pd.array([True, False])  # -> BooleanArray
# or, if Int64 is desired:
arr = pd.array([1 if b else 0 for b in [True, False]], dtype="Int64")
Defensive patterns

Strategy: type-guard

Validate before calling

import numpy as np, pandas as pd
from pandas.core.dtypes.inference import infer_dtype

def coerce_numeric(values):
    arr = np.asarray(values)
    if arr.dtype == object and infer_dtype(arr, skipna=True) == "boolean":
        raise TypeError("object bool data; use pd.array for BooleanArray")
    return arr

Type guard

def is_object_bool(values) -> bool:
    import numpy as np
    from pandas.core.dtypes.inference import infer_dtype
    arr = np.asarray(values)
    return arr.dtype == object and infer_dtype(arr, skipna=True) == "boolean"

Try / catch

try:
    arr = IntegerArray(np.asarray(values), mask)
except TypeError as e:
    if "cannot be converted to" in str(e):
        arr = pd.array(values)  # infer BooleanArray
    else:
        raise

Prevention

When it happens

Trigger: IntegerArray(np.array([True, False], dtype=object)) or _coerce_to_data_and_mask([True, False]) with dtype=None; passing a Python list of bools into a code path that expects numeric input without a dtype.

Common situations: Constructing a masked numeric array from a list/Series of booleans expecting it to become 0/1; passing object-dtype bool data into a numeric constructor.

Related errors


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