{"record":{"id":"06995c22537ec8e8","repo":"pandas-dev/pandas","slug":"input-must-be-list-like","errorCode":null,"errorMessage":"Input must be list-like","messagePattern":"Input must be list-like","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/categorical.py","lineNumber":3217,"sourceCode":"    \"\"\"\n    Factorize an input `values` into `categories` and `codes`. Preserves\n    categorical dtype in `categories`.\n\n    Parameters\n    ----------\n    values : list-like\n\n    Returns\n    -------\n    codes : ndarray\n    categories : Index\n        If `values` has a categorical dtype, then `categories` is\n        a CategoricalIndex keeping the categories and order of `values`.\n    \"\"\"\n    from pandas import CategoricalIndex\n\n    if not is_list_like(values):\n        raise TypeError(\"Input must be list-like\")\n\n    categories: Index\n\n    vdtype = getattr(values, \"dtype\", None)\n    if isinstance(vdtype, CategoricalDtype):\n        values = extract_array(values)\n        # The Categorical we want to build has the same categories\n        # as values but its codes are by def [0, ..., len(n_categories) - 1]\n        cat_codes = np.arange(len(values.categories), dtype=values.codes.dtype)\n        cat = Categorical.from_codes(cat_codes, dtype=values.dtype, validate=False)\n\n        categories = CategoricalIndex(cat)\n        codes = values.codes\n    else:\n        # The value of ordered is irrelevant since we don't use cat as such,\n        # but only the resulting categories, the order of which is independent\n        # from ordered. Set ordered to False as default. See GH #15457\n        cat = Categorical(values, ordered=False)","sourceCodeStart":3199,"sourceCodeEnd":3235,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/categorical.py#L3199-L3235","documentation":"Raised by factorize_from_iterables (the internal factorize helper) when the input values are not list-like. The function expects an iterable (list, array, Series, Index, etc.); a scalar (str, int, None) is rejected with TypeError. This guard mirrors np.array/list semantics where scalar inputs would produce wrong codes.","triggerScenarios":"Calling pd.factorize('a'), pd.Categorical.from_codes on a scalar input, groupby on a scalar key, or passing a scalar where pandas internally calls factorize_from_iterable (e.g. MultiIndex construction, pivot internals).","commonSituations":"User code that passes a single string or number where a list-like is required; programmatic callers that did not wrap scalar values in a list.","solutions":["Wrap the scalar in a list: pd.factorize([value]) instead of pd.factorize(value).","Verify the input is a list/array/Series before calling; use isinstance(x, (list, tuple, pd.Series, np.ndarray)).","Check the calling pandas API's expected input shape in its docstring."],"exampleFix":"// before\npd.factorize('a')  # TypeError: Input must be list-like\n\n// after\npd.factorize(['a','a','b'])","handlingStrategy":"validation","validationCode":"import pandas as pd\ndef ensure_list_like(v):\n    if not pd.api.types.is_list_like(v):\n        return [v]\n    return v","typeGuard":"import pandas as pd\nfrom typing import Any\n\ndef is_list_like(v: Any) -> bool:\n    return pd.api.types.is_list_like(v)","tryCatchPattern":"try:\n    pd.factorize(value)\nexcept TypeError as e:\n    if 'Input must be list-like' in str(e):\n        pd.factorize([value])\n    else:\n        raise","preventionTips":["Wrap scalars in a list before factorize or groupby keys.","Validate list-likeness for dynamic inputs."],"tags":["categorical","factorize","list-like"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}