{"record":{"id":"4e80a43ac99dedcc","repo":"pandas-dev/pandas","slug":"values-dtype-cannot-be-converted-to-name","errorCode":null,"errorMessage":"{values.dtype} cannot be converted to {name}","messagePattern":"(.+?) cannot be converted to (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/numeric.py","lineNumber":173,"sourceCode":"            values = values.astype(dtype.numpy_dtype, copy=False)\n\n        if copy:\n            values = values.copy()\n            mask = mask.copy()\n        return values, mask\n\n    original = values\n    if not copy:\n        values = np.asarray(values)\n    else:\n        values = np.array(values, copy=copy)\n    inferred_type = None\n    if values.dtype == object or is_string_dtype(values.dtype):\n        inferred_type = lib.infer_dtype(values, skipna=True)\n        if inferred_type == \"boolean\" and dtype is None:\n            # object dtype array of bools\n            name = dtype_cls.__name__.strip(\"_\")\n            raise TypeError(f\"{values.dtype} cannot be converted to {name}\")\n\n    elif values.dtype.kind == \"b\" and checker(dtype):\n        # fastpath\n        mask = np.zeros(len(values), dtype=np.bool_)\n        if not copy:\n            values = np.asarray(values, dtype=default_dtype)\n        else:\n            values = np.array(values, dtype=default_dtype, copy=copy)\n\n    elif values.dtype.kind not in \"iuf\":\n        name = dtype_cls.__name__.strip(\"_\")\n        raise TypeError(f\"{values.dtype} cannot be converted to {name}\")\n\n    if values.ndim != 1:\n        raise TypeError(\"values must be a 1D list-like\")\n\n    if mask is None:\n        if values.dtype.kind in \"iu\":","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/numeric.py#L155-L191","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use pd.array(...) which infers the correct ExtensionArray (BooleanArray for bools).","Explicitly cast the data first: np.asarray(values, dtype='int64') then construct Int64.","Pass dtype='Int64' explicitly if you want bools coerced to 0/1."],"exampleFix":"// before\narr = IntegerArray(np.array([True, False], dtype=object), ...)  # raises\n\n// after\narr = pd.array([True, False])  # -> BooleanArray\n# or, if Int64 is desired:\narr = pd.array([1 if b else 0 for b in [True, False]], dtype=\"Int64\")","handlingStrategy":"type-guard","validationCode":"import numpy as np, pandas as pd\nfrom pandas.core.dtypes.inference import infer_dtype\n\ndef coerce_numeric(values):\n    arr = np.asarray(values)\n    if arr.dtype == object and infer_dtype(arr, skipna=True) == \"boolean\":\n        raise TypeError(\"object bool data; use pd.array for BooleanArray\")\n    return arr","typeGuard":"def is_object_bool(values) -> bool:\n    import numpy as np\n    from pandas.core.dtypes.inference import infer_dtype\n    arr = np.asarray(values)\n    return arr.dtype == object and infer_dtype(arr, skipna=True) == \"boolean\"","tryCatchPattern":"try:\n    arr = IntegerArray(np.asarray(values), mask)\nexcept TypeError as e:\n    if \"cannot be converted to\" in str(e):\n        arr = pd.array(values)  # infer BooleanArray\n    else:\n        raise","preventionTips":["Use pd.array(...) which infers the right ExtensionArray.","Explicitly cast bool object arrays to int64 before NumericArray construction.","Avoid the low-level constructor in user code."],"tags":["numeric-array","coercion","boolean","type-inference"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}