pandas-dev/pandas · error · TypeError

only list-like objects are allowed to be passed to isin()…

Error message

only list-like objects are allowed to be passed to isin(), you passed a `{type(comps).__name__}`

What it means

The isin() function's first argument (comps — the values being tested for membership) must be list-like. This check uses is_list_like() which accepts lists, tuples, arrays, Series, Index, and similar iterables, but rejects scalars (int, str, bytes, None, etc.). The error message names the actual type passed so the caller can immediately see what went wrong.

Solutions

  1. Wrap the scalar in a list: s.isin([value]) instead of s.isin(value).
  2. If the argument comes from a variable that may be scalar or list-like, normalize it: values = [values] if not isinstance(values, (list, tuple, np.ndarray, pd.Series)) else values.
  3. Use np.asarray() to coerce: s.isin(np.asarray(values)).

Example fix

# before
s.isin(5)

# after
s.isin([5])
Defensive patterns

Strategy: validation

Validate before calling

from pandas.api.types import is_list_like

def safe_isin(series, values):
    if not is_list_like(values):
        values = [values]
    return series.isin(values)

Type guard

from pandas.api.types import is_list_like

def is_valid_isin_arg(value) -> bool:
    return is_list_like(value) and not isinstance(value, (str, bytes))

Try / catch

try:
    mask = s.isin(comps)
except TypeError as e:
    if "list-like" in str(e):
        mask = s.isin([comps])
    else:
        raise

Prevention

When it happens

Trigger: Calling pd.Series([1,2,3]).isin(5) — passing a single scalar int instead of a list. Calling Series.isin() or Index.isin() with a bare string where a list of strings was intended (e.g., s.isin("cat") instead of s.isin(["cat"])). Passing None or a numeric scalar as the values argument to isin().

Common situations: Forgetting to wrap a single lookup value in a list — one of the most common isin() mistakes. Dynamically constructing the isin argument from a variable that is sometimes a scalar and sometimes a list. Copying code from a context where a single value was compared with == and adapting it incorrectly to isin.

Related errors


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

Appendix: source

Thrown at pandas/core/algorithms.py:525

_FLOAT64_INT_EXACT_MAX = 2**53


def isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]:
    """
    Compute the isin boolean array.

    Parameters
    ----------
    comps : list-like
    values : list-like

    Returns
    -------
    ndarray[bool]
        Same length as `comps`.
    """
    if not is_list_like(comps):
        raise TypeError(
            "only list-like objects are allowed to be passed "
            f"to isin(), you passed a `{type(comps).__name__}`"
        )
    if not is_list_like(values):
        raise TypeError(
            "only list-like objects are allowed to be passed "
            f"to isin(), you passed a `{type(values).__name__}`"
        )

    if isinstance(values, (set, frozenset)) and len(values) > 0:
        # GH#25507: for a set of values, membership can be tested directly
        # via the set, avoiding an O(len(values)) materialization that
        # otherwise dominates when comps is much smaller than values.
        # Restrict to integer/bool comps (i.e. dtypes that cannot contain
        # NaN), since Python set membership would mis-handle the case where
        # both sides contain NaN values that are not identical.
        if isinstance(comps, (ABCSeries, ABCIndex)):
            comps_arr = comps._values

View on GitHub (pinned to 3b7651241d)