{"record":{"id":"8b08747ad0bab142","repo":"pandas-dev/pandas","slug":"only-list-like-objects-or-none-are-allowed-to-be-p","errorCode":null,"errorMessage":"Only list-like objects or None are allowed to be passed to safe_sort as codes","messagePattern":"Only list-like objects or None are allowed to be passed to safe_sort as codes","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/algorithms.py","lineNumber":1730,"sourceCode":"        except (TypeError, decimal.InvalidOperation):\n            # Previous sorters failed or were not applicable, try `_sort_mixed`\n            # which would work, but which fails for special case of 1d arrays\n            # with tuples.\n            if values.size and isinstance(values[0], tuple):\n                # error: Argument 1 to \"_sort_tuples\" has incompatible type\n                # \"Union[Index, ExtensionArray, ndarray[Any, Any]]\"; expected\n                # \"ndarray[Any, Any]\"\n                ordered = _sort_tuples(values)  # type: ignore[arg-type]\n            else:\n                ordered = _sort_mixed(values)\n\n    # codes:\n\n    if codes is None:\n        return ordered\n\n    if not is_list_like(codes):\n        raise TypeError(\n            \"Only list-like objects or None are allowed to \"\n            \"be passed to safe_sort as codes\"\n        )\n    codes = ensure_platform_int(np.asarray(codes))\n\n    # ranks[i] gives the position of values[i] in `ordered`\n    if use_counting:\n        arr = cast(\"np.ndarray\", values)\n        if arr.dtype.kind == \"i\":\n            # go through int64 so differences don't overflow narrower signed\n            #  dtypes; int64 wraparound is exact since the true differences\n            #  are within rng_size\n            shifted = arr.astype(np.int64, copy=False) - vmin\n        else:\n            # unsigned: differences always fit the unsigned dtype\n            shifted = arr - vmin\n        present = np.zeros(rng_size, dtype=bool)\n        present[shifted] = True","sourceCodeStart":1712,"sourceCodeEnd":1748,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/algorithms.py#L1712-L1748","documentation":"Raised by safe_sort when the codes argument is neither None nor list-like. codes must be an integer index array (or None) so that safe_sort can remap it to the sorted order; a scalar or dict is invalid.","triggerScenarios":"safe_sort(values, codes=5) with a scalar; safe_sort(values, codes={0: 1}) with a dict; passing None incorrectly typed.","commonSituations":"Building the codes argument from a computation that accidentally yields a scalar; forwarding unvalidated user input as codes.","solutions":["Pass an integer list/np.ndarray for codes, or None to skip remapping.","Wrap a single index in a list/array.","Validate codes with is_list_like before calling safe_sort."],"exampleFix":"# before\npd.core.algorithms.safe_sort(np.array([1, 2, 3]), codes=2)\n# after\npd.core.algorithms.safe_sort(np.array([1, 2, 3]), codes=np.array([2]))","handlingStrategy":"type-guard","validationCode":"import numpy as np\nfrom pandas.api.types import is_list_like\n\ndef safe_sort_codes(codes):\n    if codes is not None and not is_list_like(codes):\n        raise TypeError('codes must be list-like or None')\n    return None if codes is None else np.asarray(codes)","typeGuard":"from pandas.api.types import is_list_like\n\ndef valid_codes(c) -> bool:\n    return c is None or is_list_like(c)","tryCatchPattern":null,"preventionTips":["Pass None or an integer list/ndarray for codes.","Wrap single indices in a list/array.","Validate codes with is_list_like at boundaries."],"tags":["safe-sort","codes","arraylike","validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}