pandas-dev/pandas · error · TypeError

Need to pass bool-like values

Error message

Need to pass bool-like values

What it means

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'.

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 71959b8cb9)

Solutions

  1. Clean the data so only 0/1 (and NA) values remain before converting.
  2. Map non-0/1 values explicitly, e.g. (s != 0) to derive a true boolean, then build the BooleanArray.
  3. Choose a numeric dtype (Int64/Float64) instead of 'boolean' if values are not binary.
  4. Clip/round values to 0/1 only if that semantics is intended.

Example fix

# before
pd.array(np.array([0, 1, 2]), dtype="boolean")  # raises

# after
pd.array(np.array([0, 1, 2]) == 1, dtype="boolean")
Defensive patterns

Strategy: validation

Validate before calling

def to_bool_array_from_int(arr):
    import numpy as np
    arr = np.asarray(arr)
    if arr.dtype.kind in "iufcb":
        valid = np.isin(arr, [0, 1]) | np.isnan(arr.astype(float, copy=False))
        if not valid.all():
            raise TypeError("integer array contains values other than 0/1")
    return arr

Type guard

def is_binary_numeric(arr) -> bool:
    import numpy as np
    arr = np.asarray(arr)
    if arr.dtype.kind not in "iufcb":
        return False
    mask = np.isnan(arr.astype(float, copy=False))
    return bool(np.isin(arr[~mask], [0, 1]).all())

Try / catch

try:
    ba = pd.array(arr, dtype="boolean")
except TypeError as e:
    if "bool-like" in str(e):
        import numpy as np
        ba = pd.array(np.asarray(arr) == 1, dtype="boolean")
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Converting an integer flag column that legitimately contains 2/3/... to boolean; dirty numeric data; assuming any integer column maps cleanly to bool.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/32f079f6c5d658d2. Report an issue: GitHub.