{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/numeric.py#L155-L191","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n_coerce_to_data_and_mask(np.array([True, False], dtype=object), None, ...)   # raises\n// after\n_coerce_to_data_and_mask(np.array([True, False], dtype=object), 'Int64', ...)","handlingStrategy":"validation","validationCode":"import numpy as np\nfrom pandas.core.lib import infer_dtype\nif values.dtype == object and infer_dtype(values, skipna=True) == 'boolean' and dtype is None:\n    dtype = 'Int64'   # or 'boolean', per intent","typeGuard":"def object_array_is_safe_for_numeric(values, dtype) -> bool:\n    from pandas.core.lib import infer_dtype\n    return not (values.dtype == object and infer_dtype(values, skipna=True) == 'boolean' and dtype is None)","tryCatchPattern":"try:\n    _coerce_to_data_and_mask(values, dtype, ...)\nexcept TypeError as e:\n    if 'cannot be converted' in str(e):\n        _coerce_to_data_and_mask(values, 'Int64', ...)\n    else:\n        raise","preventionTips":["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."],"tags":["pandas","numeric-array","dtype","object","boolean"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}