pandas-dev/pandas · error · TypeError
only list-like objects are allowed to be passed to isin(), y
Error message
only list-like objects are allowed to be passed to isin(), you passed a `{type(comps).__name__}` What it means
Raised by pandas.core.algorithms.isin when the comps argument (the values being tested for membership) is not list-like. Series/Index.isin route their underlying values through this function; a non-list-like comps cannot be tested element-wise, so pandas rejects it up front.
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 71959b8cb9)
Solutions
- Pass a list-like for comps (wrap a scalar in a list).
- Prefer the public Series.isin(values) / Index.isin(values) which validate at the boundary.
- Add an is_list_like check before invoking the internal function.
Example fix
# before from pandas.core.algorithms import isin mask = isin(5, [1, 2, 5]) # after mask = isin([5], [1, 2, 5])
Defensive patterns
Strategy: type-guard
Validate before calling
from pandas.api.types import is_list_like
def safe_isin(comps, values):
if not is_list_like(comps):
raise TypeError(f'comps must be list-like, got {type(comps).__name__}')
return pd.core.algorithms.isin(comps, values) Type guard
from pandas.api.types import is_list_like
def is_listlike(x) -> bool:
return is_list_like(x) Prevention
- Always pass a list-like for the comps (left) side of isin.
- Prefer the public Series.isin / Index.isin over the internal function.
- Validate with is_list_like at boundaries.
When it happens
Trigger: Calling the internal pd.core.algorithms.isin with a scalar comps; indirectly, paths that build the comps from a scalar before reaching isin.
Common situations: Calling isin via the internal API with accidentally-scalar data; refactors that pass a single value instead of a sequence as the left-hand side.
Related errors
- only list-like objects are allowed to be passed to isin(), y
- {func_name} requires a Series, Index, ExtensionArray, np.nda
- pd.api.extensions.take requires a numpy.ndarray, ExtensionAr
- Only np.ndarray, ExtensionArray, and Index objects are allow
- Only list-like objects or None are allowed to be passed to s
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/74ed64d417ef2f17.
Report an issue: GitHub.