pandas-dev/pandas · error · TypeError

Cannot use quantile with bool dtype

Error message

Cannot use quantile with bool dtype

What it means

Raised by ExtensionArray._groupby_quantile when the grouped array has a boolean dtype. Boolean data has no meaningful ordering for percentile interpolation, so pandas blocks quantile computation explicitly (separate from the string/object check). This guard runs before any numeric conversion of the values.

Solutions

  1. Filter boolean columns out before calling quantile: select only integer/float dtypes.
  2. Cast the boolean column to int if you treat True=1/False=0: df['col'] = df['col'].astype('Int64').
  3. Use a boolean-appropriate aggregation instead (sum for count of True, mean for proportion True).
  4. Drop the boolean column from the groupby column selection.

Example fix

// before
df.groupby('key')['is_active'].quantile(0.5)
// after
df.groupby('key')['is_active'].astype('Int64').quantile(0.5)
Defensive patterns

Strategy: validation

Validate before calling

from pandas.api.types import is_bool_dtype
if is_bool_dtype(df[col]):
    raise TypeError(f'{col} is bool; quantile unsupported')

Type guard

def supports_quantile(s) -> bool:
    from pandas.api.types import is_bool_dtype, is_numeric_dtype
    return is_numeric_dtype(s) and not is_bool_dtype(s)

Try / catch

try:
    df.groupby('key')[col].quantile(q)
except TypeError as e:
    if 'bool dtype' in str(e):
        ...

Prevention

When it happens

Trigger: Calling groupby(...).quantile() on a column of dtype bool or pandas 'boolean' (nullable BooleanArray). Reached through agg({'col': 'quantile'}), SeriesGroupBy.quantile, or describe() on a boolean column inside a groupby.

Common situations: Boolean flag columns included in a broad select_dtypes pipeline that happens to capture bools; nullable 'boolean' arrays from pd.array(..., dtype='boolean'); aggregating survey/experiment flag data with a quantile template written for numeric metrics.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/8901f6b7b60b69ae. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/base.py:3155

        interpolation : {'linear', 'lower', 'higher', 'nearest', 'midpoint'}
        ids : np.ndarray[np.intp]
            Group labels.
        ngroups : int
        starts : np.ndarray[int64]
        ends : np.ndarray[int64]

        Returns
        -------
        np.ndarray or ExtensionArray
        """
        from pandas.core.arrays.string_ import StringDtype

        if isinstance(self.dtype, StringDtype) or is_object_dtype(self.dtype):
            raise TypeError(
                f"dtype '{self.dtype}' does not support operation 'quantile'"
            )
        if is_bool_dtype(self.dtype):
            raise TypeError("Cannot use quantile with bool dtype")

        mask = np.asarray(isna(self))
        nqs = len(qs)

        if is_integer_dtype(self.dtype) or is_float_dtype(self.dtype):
            vals = self.to_numpy(dtype=float, na_value=np.nan)
        else:
            vals = np.asarray(self)

        inference: np.dtype | None = (
            np.dtype(np.int64) if is_integer_dtype(self.dtype) else None
        )

        out = np.empty((ngroups, nqs), dtype=np.float64)
        libgroupby.group_quantile(
            out,
            values=vals,
            mask=mask,  # type: ignore[arg-type]

View on GitHub (pinned to 3b7651241d)