pandas-dev/pandas · error · TypeError
Need to pass bool-like values
Error message
Need to pass bool-like values
What it means
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.
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.
Example fix
// before pd.array(np.array([0, 2, 1]), dtype='boolean') // after arr = np.array([0, 2, 1]) arr = np.where(arr != 0, 1, 0) pd.array(arr, dtype='boolean')
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
if isinstance(values, np.ndarray) and values.dtype.kind in 'iufcb':
non_na = ~np.isnan(values) if values.dtype.kind == 'f' else np.ones_like(values, dtype=bool)
if not np.all(np.isin(values[non_na], [0, 1])):
raise TypeError('non-0/1 values present; sanitize first') Type guard
def is_zero_one_array(arr) -> bool:
import numpy as np
return isinstance(arr, np.ndarray) and np.all(np.isin(arr, [0, 1])) Try / catch
try:
pd.array(arr, dtype='boolean')
except TypeError as e:
if 'bool-like' in str(e):
arr = np.where(arr != 0, 1, 0)
... Prevention
- Restrict integer arrays to {0, 1} before boolean coercion.
- Map sentinel integers explicitly.
When it happens
Trigger: 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}.
Common situations: 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.
Related errors
- values.shape and mask.shape must match
- cannot pass mask for BooleanArray input
- values should be boolean numpy array. Use the 'pd.array'…
- can only insert Interval objects and NA into an…
- Cannot apply ufunc to mixed DataFrame and Series inputs.
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/32f079f6c5d658d2.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/boolean.py:221
if copy:
values = values.copy()
mask = mask.copy()
return values, mask
mask_values = None
if isinstance(values, np.ndarray) and values.dtype == np.bool_:
if copy:
values = values.copy()
elif isinstance(values, np.ndarray) and values.dtype.kind in "iufcb":
mask_values = isna(values)
values_bool = np.zeros(len(values), dtype=bool)
values_bool[~mask_values] = values[~mask_values].astype(bool)
if not np.all(
values_bool[~mask_values].astype(values.dtype) == values[~mask_values]
):
raise TypeError("Need to pass bool-like values")
values = values_bool
else:
values_object = np.asarray(values, dtype=object)
inferred_dtype = lib.infer_dtype(values_object, skipna=True)
integer_like = ("floating", "integer", "mixed-integer-float")
if inferred_dtype not in ("boolean", "empty", *integer_like):
raise TypeError("Need to pass bool-like values")
# mypy does not narrow the type of mask_values to npt.NDArray[np.bool_]
# within this branch, it assumes it can also be None
mask_values = cast("npt.NDArray[np.bool_]", isna(values_object))
values = np.zeros(len(values), dtype=bool)
values[~mask_values] = values_object[~mask_values].astype(bool)
# if the values were integer-like, validate it were actually 0/1's
if (inferred_dtype in integer_like) and not (View on GitHub (pinned to 3b7651241d)