{"record":{"id":"74ed64d417ef2f17","repo":"pandas-dev/pandas","slug":"only-list-like-objects-are-allowed-to-be-passed-to","errorCode":null,"errorMessage":"only list-like objects are allowed to be passed to isin(), you passed a `{type(comps).__name__}`","messagePattern":"only list-like objects are allowed to be passed to isin\\(\\), you passed a `(.+?)`","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/algorithms.py","lineNumber":525,"sourceCode":"_FLOAT64_INT_EXACT_MAX = 2**53\n\n\ndef isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]:\n    \"\"\"\n    Compute the isin boolean array.\n\n    Parameters\n    ----------\n    comps : list-like\n    values : list-like\n\n    Returns\n    -------\n    ndarray[bool]\n        Same length as `comps`.\n    \"\"\"\n    if not is_list_like(comps):\n        raise TypeError(\n            \"only list-like objects are allowed to be passed \"\n            f\"to isin(), you passed a `{type(comps).__name__}`\"\n        )\n    if not is_list_like(values):\n        raise TypeError(\n            \"only list-like objects are allowed to be passed \"\n            f\"to isin(), you passed a `{type(values).__name__}`\"\n        )\n\n    if isinstance(values, (set, frozenset)) and len(values) > 0:\n        # GH#25507: for a set of values, membership can be tested directly\n        # via the set, avoiding an O(len(values)) materialization that\n        # otherwise dominates when comps is much smaller than values.\n        # Restrict to integer/bool comps (i.e. dtypes that cannot contain\n        # NaN), since Python set membership would mis-handle the case where\n        # both sides contain NaN values that are not identical.\n        if isinstance(comps, (ABCSeries, ABCIndex)):\n            comps_arr = comps._values","sourceCodeStart":507,"sourceCodeEnd":543,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/algorithms.py#L507-L543","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","solutions":["Wrap the scalar in a list: s.isin([value]) instead of s.isin(value).","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.","Use np.asarray() to coerce: s.isin(np.asarray(values))."],"exampleFix":"# before\ns.isin(5)\n\n# after\ns.isin([5])","handlingStrategy":"validation","validationCode":"from pandas.api.types import is_list_like\n\ndef safe_isin(series, values):\n    if not is_list_like(values):\n        values = [values]\n    return series.isin(values)","typeGuard":"from pandas.api.types import is_list_like\n\ndef is_valid_isin_arg(value) -> bool:\n    return is_list_like(value) and not isinstance(value, (str, bytes))","tryCatchPattern":"try:\n    mask = s.isin(comps)\nexcept TypeError as e:\n    if \"list-like\" in str(e):\n        mask = s.isin([comps])\n    else:\n        raise","preventionTips":["Always wrap single values in a list when calling isin(): s.isin([val]).","Create a helper that normalizes scalars to lists before isin calls.","Remember that strings are iterable but almost always need wrapping for isin."],"tags":["pandas","isin","type-validation","typeerror","array-like"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}