{"record":{"id":"076ffe84b2c84aea","repo":"pandas-dev/pandas","slug":"func-name-requires-a-series-index-extensionarr","errorCode":null,"errorMessage":"{func_name} requires a Series, Index, ExtensionArray, np.ndarray or NumpyExtensionArray got {type(values).__name__}.","messagePattern":"(.+?) requires a Series, Index, ExtensionArray, np\\.ndarray or NumpyExtensionArray got (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/algorithms.py","lineNumber":239,"sourceCode":"\n    # error: Incompatible return value type\n    # (got \"ndarray[tuple[Any, ...], dtype[Any]]\",\n    # expected \"ExtensionArray\")\n    return values.astype(dtype, copy=False)  # type: ignore[return-value]\n\n\ndef _ensure_arraylike(values, func_name: str) -> ArrayLike:\n    \"\"\"\n    ensure that we are arraylike if not already\n    \"\"\"\n    if not isinstance(\n        values,\n        (ABCIndex, ABCSeries, ABCExtensionArray, np.ndarray, ABCNumpyExtensionArray),\n    ):\n        # GH#52986\n        if func_name != \"isin-targets\":\n            # Make an exception for the comps argument in isin.\n            raise TypeError(\n                f\"{func_name} requires a Series, Index, \"\n                f\"ExtensionArray, np.ndarray or NumpyExtensionArray \"\n                f\"got {type(values).__name__}.\"\n            )\n\n        inferred = lib.infer_dtype(values, skipna=False)\n        if inferred in [\"mixed\", \"string\", \"mixed-integer\"]:\n            # \"mixed-integer\" to ensure we do not cast [\"ss\", 42] to str GH#22160\n            if isinstance(values, tuple):\n                values = list(values)\n            values = construct_1d_object_array_from_listlike(values)\n        else:\n            values = np.asarray(values)\n    return values\n\n\n_hashtables = {\n    \"complex128\": htable.Complex128HashTable,","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/algorithms.py#L221-L257","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\npd.factorize(5)\n# after\npd.factorize([5])","handlingStrategy":"type-guard","validationCode":"from pandas.api.types import is_list_like\nimport numpy as np\n\ndef to_arraylike(x):\n    if not is_list_like(x):\n        raise TypeError(f'expected array-like, got {type(x).__name__}')\n    return np.asarray(x)","typeGuard":"from pandas.api.types import is_list_like\nimport numpy as np, pandas as pd\n\ndef is_arraylike(x) -> bool:\n    return isinstance(x, (pd.Series, pd.Index, np.ndarray)) or is_list_like(x)","tryCatchPattern":null,"preventionTips":["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."],"tags":["arraylike","validation","factorize","type-error"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}