pandas-dev/pandas · error · ValueError
searchsorted requires array to be sorted, which is impossibl
Error message
searchsorted requires array to be sorted, which is impossible with NAs present.
What it means
StringArray.searchsorted requires a sorted array, but missing values (pd.NA or np.nan) have no defined sort order, making a correct result impossible. The method checks only the first and last positions (or sorter[0]/sorter[-1] when a sorter is given) for NA, since NA must be confined to the ends in sorted data, and raises ValueError if found.
Source
Thrown at pandas/core/arrays/string_.py:1198
"""
# GH#65837: avoid O(n) scan; NA confined to array ends in sorted data.
# When sorter is given, the sorted order is ndarray[sorter], so check
# the first/last positions via sorter instead of raw ndarray positions.
ndarray = self._ndarray
if len(ndarray):
if sorter is None:
has_na = libmissing.checknull(ndarray[0]) or libmissing.checknull(
ndarray[-1]
)
else:
has_na = libmissing.checknull(
ndarray[sorter[0]]
) or libmissing.checknull(ndarray[sorter[-1]])
else:
has_na = False
if has_na:
raise ValueError(
"searchsorted requires array to be sorted, which is impossible "
"with NAs present."
)
return super().searchsorted(value=value, side=side, sorter=sorter)
def _cmp_method(self, other, op):
from pandas.arrays import (
ArrowExtensionArray,
BooleanArray,
)
if (
isinstance(other, BaseStringArray)
and self.dtype.na_value is not libmissing.NA
and other.dtype.na_value is libmissing.NA
):
# NA has priority of NaN semantics
return op(self.astype(other.dtype, copy=False), other)View on GitHub (pinned to 71959b8cb9)
Solutions
- Drop NAs before searching: clean = arr[~arr.isna()].
- Fill missing values with a concrete string if a sentinel is acceptable.
- Sort and verify no NAs at the ends before calling searchsorted.
Example fix
// before
string_array.searchsorted('x')
// after
clean = string_array[~string_array.isna()]
clean.searchsorted('x') Defensive patterns
Strategy: validation
Validate before calling
clean = string_array[~string_array.isna()]
result = clean.searchsorted('x') Type guard
import pandas as pd
def has_no_na(arr) -> bool:
return not bool(pd.isna(arr).any()) Prevention
- Drop or fill NAs before calling searchsorted.
- Verify the array is sorted and NA-free at the ends before searching.
- Use a cleaned/sorted copy for binary-search lookups.
When it happens
Trigger: Calling string_array.searchsorted('x') or searchsorted(['a','z']) on a StringArray that contains pd.NA/np.nan at the leading or trailing positions, or passing a sorter whose endpoints point at NA values.
Common situations: Binary-search lookups on string data loaded from CSVs with missing fields; using searchsorted on an uncleaned column; sorted indexes that still carry nulls.
Related errors
- searchsorted requires array to be sorted, which is impossibl
- searchsorted requires array to be sorted, which is impossibl
- invalid na_position: {na_position!r}
- missing values must be missing in the same location both lef
- cannot convert to '{dtype}'-dtype NumPy array with missing v
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/f9d465f22ec115b2.
Report an issue: GitHub.