pandas-dev/pandas · error · TypeError
Only list-like objects or None are allowed to be passed to s
Error message
Only list-like objects or None are allowed to be passed to safe_sort as codes
What it means
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.
Source
Thrown at pandas/core/algorithms.py:1730
except (TypeError, decimal.InvalidOperation):
# Previous sorters failed or were not applicable, try `_sort_mixed`
# which would work, but which fails for special case of 1d arrays
# with tuples.
if values.size and isinstance(values[0], tuple):
# error: Argument 1 to "_sort_tuples" has incompatible type
# "Union[Index, ExtensionArray, ndarray[Any, Any]]"; expected
# "ndarray[Any, Any]"
ordered = _sort_tuples(values) # type: ignore[arg-type]
else:
ordered = _sort_mixed(values)
# codes:
if codes is None:
return ordered
if not is_list_like(codes):
raise TypeError(
"Only list-like objects or None are allowed to "
"be passed to safe_sort as codes"
)
codes = ensure_platform_int(np.asarray(codes))
# ranks[i] gives the position of values[i] in `ordered`
if use_counting:
arr = cast("np.ndarray", values)
if arr.dtype.kind == "i":
# go through int64 so differences don't overflow narrower signed
# dtypes; int64 wraparound is exact since the true differences
# are within rng_size
shifted = arr.astype(np.int64, copy=False) - vmin
else:
# unsigned: differences always fit the unsigned dtype
shifted = arr - vmin
present = np.zeros(rng_size, dtype=bool)
present[shifted] = TrueView on GitHub (pinned to 71959b8cb9)
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.
Example fix
# before pd.core.algorithms.safe_sort(np.array([1, 2, 3]), codes=2) # after pd.core.algorithms.safe_sort(np.array([1, 2, 3]), codes=np.array([2]))
Defensive patterns
Strategy: type-guard
Validate before calling
import numpy as np
from pandas.api.types import is_list_like
def safe_sort_codes(codes):
if codes is not None and not is_list_like(codes):
raise TypeError('codes must be list-like or None')
return None if codes is None else np.asarray(codes) Type guard
from pandas.api.types import is_list_like
def valid_codes(c) -> bool:
return c is None or is_list_like(c) Prevention
- Pass None or an integer list/ndarray for codes.
- Wrap single indices in a list/array.
- Validate codes with is_list_like at boundaries.
When it happens
Trigger: safe_sort(values, codes=5) with a scalar; safe_sort(values, codes={0: 1}) with a dict; passing None incorrectly typed.
Common situations: Building the codes argument from a computation that accidentally yields a scalar; forwarding unvalidated user input as codes.
Related errors
- Only np.ndarray, ExtensionArray, and Index objects are allow
- values should be unique if codes is not None
- {func_name} requires a Series, Index, ExtensionArray, np.nda
- only list-like objects are allowed to be passed to isin(), y
- only list-like objects are allowed to be passed to isin(), y
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/8b08747ad0bab142.
Report an issue: GitHub.