pandas-dev/pandas · error · TypeError
{func_name} requires a Series, Index, ExtensionArray, np.nda
Error message
{func_name} requires a Series, Index, ExtensionArray, np.ndarray or NumpyExtensionArray got {type(values).__name__}. What it means
Raised by _ensure_arraylike in pandas.core.algorithms. Many internal algorithms (factorize, unique, value_counts, etc.) coerce inputs to array-like before working; if the value is not a Series, Index, ExtensionArray, np.ndarray or NumpyExtensionArray, this TypeError is raised (with a special carve-out for the isin targets path). It guards the algorithm layer against scalars and arbitrary objects.
Source
Thrown at pandas/core/algorithms.py:239
# error: Incompatible return value type
# (got "ndarray[tuple[Any, ...], dtype[Any]]",
# expected "ExtensionArray")
return values.astype(dtype, copy=False) # type: ignore[return-value]
def _ensure_arraylike(values, func_name: str) -> ArrayLike:
"""
ensure that we are arraylike if not already
"""
if not isinstance(
values,
(ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray, ABCNumpyExtensionArray),
):
# GH#52986
if func_name != "isin-targets":
# Make an exception for the comps argument in isin.
raise TypeError(
f"{func_name} requires a Series, Index, "
f"ExtensionArray, np.ndarray or NumpyExtensionArray "
f"got {type(values).__name__}."
)
inferred = lib.infer_dtype(values, skipna=False)
if inferred in ["mixed", "string", "mixed-integer"]:
# "mixed-integer" to ensure we do not cast ["ss", 42] to str GH#22160
if isinstance(values, tuple):
values = list(values)
values = construct_1d_object_array_from_listlike(values)
else:
values = np.asarray(values)
return values
_hashtables = {
"complex128": htable.Complex128HashTable,View on GitHub (pinned to 71959b8cb9)
Solutions
- Wrap the value in a list/Series/np.ndarray before calling the algorithm.
- Validate the input is array-like (is_list_like) and raise a clearer error at your boundary.
- Convert dicts to a Series when a mapping of values is the intent.
Example fix
# before pd.factorize(5) # after pd.factorize([5])
Defensive patterns
Strategy: type-guard
Validate before calling
from pandas.api.types import is_list_like
import numpy as np
def to_arraylike(x):
if not is_list_like(x):
raise TypeError(f'expected array-like, got {type(x).__name__}')
return np.asarray(x) Type guard
from pandas.api.types import is_list_like
import numpy as np, pandas as pd
def is_arraylike(x) -> bool:
return isinstance(x, (pd.Series, pd.Index, np.ndarray)) or is_list_like(x) Prevention
- Coerce user input with np.asarray or pd.Series at API boundaries.
- Guard with is_list_like before algorithm calls.
- Reject dicts/scalars explicitly with a clear message.
When it happens
Trigger: Passing a Python scalar or dict directly to an algorithm such as pd.factorize(5), pd.unique({1:2}), or pd.core.algorithms functions with a non-arraylike; calling a public API whose implementation routes the first argument through _ensure_arraylike.
Common situations: Constructing pipelines that forward user input straight into factorize/unique without coercion; data that arrives as a single scalar where a 1-D sequence was expected; dicts being passed where an array-like was intended.
Related errors
- only list-like objects are allowed to be passed to isin(), y
- only list-like objects are allowed to be passed to isin(), y
- 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/076ffe84b2c84aea.
Report an issue: GitHub.