{"record":{"id":"882bb59087b22562","repo":"pandas-dev/pandas","slug":"only-np-ndarray-extensionarray-and-index-objects","errorCode":null,"errorMessage":"Only np.ndarray, ExtensionArray, and Index objects are allowed to be passed to safe_sort as values","messagePattern":"Only np\\.ndarray, ExtensionArray, and Index objects are allowed to be passed to safe_sort as values","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/algorithms.py","lineNumber":1669,"sourceCode":"\n    Returns\n    -------\n    ordered : AnyArrayLike\n        Sorted ``values``\n    new_codes : ndarray\n        Reordered ``codes``; returned when ``codes`` is not None.\n\n    Raises\n    ------\n    TypeError\n        * If ``values`` is not list-like or if ``codes`` is neither None\n        nor list-like\n        * If ``values`` cannot be sorted\n    ValueError\n        * If ``codes`` is not None and ``values`` contain duplicates.\n    \"\"\"\n    if not isinstance(values, (np.ndarray, ABCExtensionArray, ABCIndex)):\n        raise TypeError(\n            \"Only np.ndarray, ExtensionArray, and Index objects are allowed to \"\n            \"be passed to safe_sort as values\"\n        )\n\n    sorter = None\n    ordered: AnyArrayLike\n\n    # For integer values spanning a modest range, the codes remapping below\n    #  can rank each value within that range (a counting sort) instead of\n    #  argsorting.\n    use_counting = False\n    vmin = rng_size = 0\n    if (\n        codes is not None\n        and isinstance(values, np.ndarray)\n        and values.dtype.kind in \"iu\"\n        and len(values) >= _MIN_COUNTING_SORT_LEN\n    ):","sourceCodeStart":1651,"sourceCodeEnd":1687,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/algorithms.py#L1651-L1687","documentation":"Raised by pandas.core.algorithms.safe_sort when the values argument is not an np.ndarray, ExtensionArray, or Index. safe_sort needs typed array semantics to sort and remap codes; a plain Python list/tuple/set is not accepted.","triggerScenarios":"Calling pd.core.algorithms.safe_sort([3, 1, 2]) with a list; passing a set or scalar as values; internal callers (e.g. unique/factorize) that forgot to coerce to an array.","commonSituations":"Using safe_sort directly with native Python containers; refactors that removed an np.asarray step upstream.","solutions":["Convert values to np.ndarray or a pandas Index/Series first.","Use np.sort for plain lists when you do not need code remapping.","Validate the type at your boundary."],"exampleFix":"# before\npd.core.algorithms.safe_sort([3, 1, 2])\n# after\npd.core.algorithms.safe_sort(np.array([3, 1, 2]))","handlingStrategy":"type-guard","validationCode":"import numpy as np, pandas as pd\n\ndef safe_sort_values(values, codes=None, **kw):\n    if not isinstance(values, (np.ndarray, pd.Index, pd.api.extensions.ExtensionArray)):\n        values = np.asarray(values)\n    return pd.core.algorithms.safe_sort(values, codes, **kw)","typeGuard":"import numpy as np, pandas as pd\n\ndef is_safe_sort_values(v) -> bool:\n    return isinstance(v, (np.ndarray, pd.Index, pd.api.extensions.ExtensionArray))","tryCatchPattern":null,"preventionTips":["Convert lists to np.ndarray before safe_sort.","Use np.sort for plain lists when code remapping isn't needed.","Validate the values type at the boundary."],"tags":["safe-sort","arraylike","validation","sorting"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}