pandas-dev/pandas · error · TypeError

Cannot use quantile with bool dtype

Error message

Cannot use quantile with bool dtype

What it means

ExtensionArray._groupby_quantile (base.py:3155) rejects boolean dtype with TypeError. Although booleans are technically 0/1, computing interpolation-based quantiles on a binary domain is statistically meaningless, so pandas forbids it explicitly.

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

Solutions

  1. Exclude boolean columns before quantile: operate on numeric columns only.
  2. Cast the boolean column to int if a 0/1 quantile is genuinely wanted: s.astype('Int64').groupby(key).quantile().
  3. Drop the boolean column from the groupby quantile target.
  4. Use a boolean-appropriate aggregation (any/all/sum-of-true).

Example fix

# before
df.groupby("id")["flag"].quantile()  # flag is 'boolean' -> raises

# after
df["flag"].astype("Int64").groupby(df["id"]).quantile()
Defensive patterns

Strategy: validation

Validate before calling

def safe_groupby_quantile(df, key, q):
    import pandas as pd
    num = df.select_dtypes("number")
    return num.groupby(df[key]).quantile(q)

Type guard

def is_quantile_supported(dtype) -> bool:
    import pandas as pd
    return pd.api.types.is_numeric_dtype(dtype) and not pd.api.types.is_bool_dtype(dtype)

Try / catch

try:
    df.groupby("id")["flag"].quantile()
except TypeError as e:
    if "bool dtype" in str(e):
        df["flag"].astype("Int64").groupby(df["id"]).quantile()
    else:
        raise

Prevention

When it happens

Trigger: Calling df.groupby(key).quantile(...) or SeriesGroupBy.quantile on a 'boolean' (nullable bool) column, or a plain bool column routed through this path.

Common situations: Running groupby().quantile() over an entire DataFrame that includes a boolean flag column; flag/indicator columns inadvertently included in numeric aggregation pipelines.

Related errors


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