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
- 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)).
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
- 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.
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
- only list-like objects are allowed to be passed to isin()…
- requires a Series, Index, ExtensionArray, np.ndarray or…
- Only list-like objects or None are allowed to be passed to…
- Only np.ndarray, ExtensionArray, and Index objects are…
- pd.api.extensions.take requires a numpy.ndarray…
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._valuesView on GitHub (pinned to 3b7651241d)