{"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":"exception","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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/boolean.py#L203-L239","documentation":"Raised by coerce_to_array when the input is a numpy array of integer/unsigned/float/complex/byte kind whose values cannot be losslessly cast to bool. The check casts back to the original dtype and compares: any value other than 0/1 (e.g. 2, -1, 0.5) fails. This prevents silently treating arbitrary numbers as truthy.","triggerScenarios":"pd.array(np.array([0, 2]), dtype='boolean'), BooleanArray construction via coerce_to_array with an int8/int16/int32/int64/uint/float/complex/bytes array containing values outside {0, 1}.","commonSituations":"Storing 0/1 integer flags and accidentally including a 2 or -1; reading data where a 'boolean' column has sentinel codes beyond 0/1; passing a float array with NaN handled outside pandas.","solutions":["Sanitize the input so every non-NA value is 0 or 1 before calling coerce_to_array.","Map non-0/1 values explicitly: np.where(arr != 0, 1, 0) if you genuinely mean non-zero -> True.","Cast through object dtype with pd.array(arr.astype(object), dtype='boolean') only if you accept pandas' bool inference."],"exampleFix":"// before\npd.array(np.array([0, 2, 1]), dtype='boolean')\n// after\narr = np.array([0, 2, 1])\narr = np.where(arr != 0, 1, 0)\npd.array(arr, dtype='boolean')","handlingStrategy":"validation","validationCode":"import numpy as np\nif isinstance(values, np.ndarray) and values.dtype.kind in 'iufcb':\n    non_na = ~np.isnan(values) if values.dtype.kind == 'f' else np.ones_like(values, dtype=bool)\n    if not np.all(np.isin(values[non_na], [0, 1])):\n        raise TypeError('non-0/1 values present; sanitize first')","typeGuard":"def is_zero_one_array(arr) -> bool:\n    import numpy as np\n    return isinstance(arr, np.ndarray) and np.all(np.isin(arr, [0, 1]))","tryCatchPattern":"try:\n    pd.array(arr, dtype='boolean')\nexcept TypeError as e:\n    if 'bool-like' in str(e):\n        arr = np.where(arr != 0, 1, 0)\n        ...","preventionTips":["Restrict integer arrays to {0, 1} before boolean coercion.","Map sentinel integers explicitly."],"tags":["boolean","coerce","numpy","integer","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}