{"record":{"id":"32f079f6c5d658d2","repo":"pandas-dev/pandas","slug":"need-to-pass-bool-like-values","errorCode":null,"errorMessage":"Need to pass bool-like values","messagePattern":"Need to pass bool-like values","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/boolean.py","lineNumber":221,"sourceCode":"        if copy:\n            values = values.copy()\n            mask = mask.copy()\n        return values, mask\n\n    mask_values = None\n    if isinstance(values, np.ndarray) and values.dtype == np.bool_:\n        if copy:\n            values = values.copy()\n    elif isinstance(values, np.ndarray) and values.dtype.kind in \"iufcb\":\n        mask_values = isna(values)\n\n        values_bool = np.zeros(len(values), dtype=bool)\n        values_bool[~mask_values] = values[~mask_values].astype(bool)\n\n        if not np.all(\n            values_bool[~mask_values].astype(values.dtype) == values[~mask_values]\n        ):\n            raise TypeError(\"Need to pass bool-like values\")\n\n        values = values_bool\n    else:\n        values_object = np.asarray(values, dtype=object)\n\n        inferred_dtype = lib.infer_dtype(values_object, skipna=True)\n        integer_like = (\"floating\", \"integer\", \"mixed-integer-float\")\n        if inferred_dtype not in (\"boolean\", \"empty\", *integer_like):\n            raise TypeError(\"Need to pass bool-like values\")\n\n        # mypy does not narrow the type of mask_values to npt.NDArray[np.bool_]\n        # within this branch, it assumes it can also be None\n        mask_values = cast(\"npt.NDArray[np.bool_]\", isna(values_object))\n        values = np.zeros(len(values), dtype=bool)\n        values[~mask_values] = values_object[~mask_values].astype(bool)\n\n        # if the values were integer-like, validate it were actually 0/1's\n        if (inferred_dtype in integer_like) and not (","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/boolean.py#L203-L239","documentation":"In coerce_to_array (boolean.py:221), when the input is a numpy integer/float/complex ndarray, pandas attempts to reinterpret values as boolean (0/1) and validates that casting back to the original dtype is lossless; if any value is not 0/1/NA it raises TypeError 'Need to pass bool-like values'.","triggerScenarios":"pd.array(np.array([0,1,2]), dtype='boolean'), BooleanArray._from_sequence on an int ndarray containing values other than 0/1, or astype('boolean') on such a numeric array.","commonSituations":"Converting an integer flag column that legitimately contains 2/3/... to boolean; dirty numeric data; assuming any integer column maps cleanly to bool.","solutions":["Clean the data so only 0/1 (and NA) values remain before converting.","Map non-0/1 values explicitly, e.g. (s != 0) to derive a true boolean, then build the BooleanArray.","Choose a numeric dtype (Int64/Float64) instead of 'boolean' if values are not binary.","Clip/round values to 0/1 only if that semantics is intended."],"exampleFix":"# before\npd.array(np.array([0, 1, 2]), dtype=\"boolean\")  # raises\n\n# after\npd.array(np.array([0, 1, 2]) == 1, dtype=\"boolean\")","handlingStrategy":"validation","validationCode":"def to_bool_array_from_int(arr):\n    import numpy as np\n    arr = np.asarray(arr)\n    if arr.dtype.kind in \"iufcb\":\n        valid = np.isin(arr, [0, 1]) | np.isnan(arr.astype(float, copy=False))\n        if not valid.all():\n            raise TypeError(\"integer array contains values other than 0/1\")\n    return arr","typeGuard":"def is_binary_numeric(arr) -> bool:\n    import numpy as np\n    arr = np.asarray(arr)\n    if arr.dtype.kind not in \"iufcb\":\n        return False\n    mask = np.isnan(arr.astype(float, copy=False))\n    return bool(np.isin(arr[~mask], [0, 1]).all())","tryCatchPattern":"try:\n    ba = pd.array(arr, dtype=\"boolean\")\nexcept TypeError as e:\n    if \"bool-like\" in str(e):\n        import numpy as np\n        ba = pd.array(np.asarray(arr) == 1, dtype=\"boolean\")\n    else:\n        raise","preventionTips":["Verify integer arrays contain only 0/1 before boolean conversion","Use (arr == 1) to derive booleans","Choose Int64 if values are not binary"],"tags":["boolean","coercion","ndarray","validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}