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
searchsorted requires the array to be sorted, but NA values have no defined ordering, so an array containing NAs cannot be considered sorted. pandas raises ValueError eagerly rather than returning a meaningless insertion index.
Source
Thrown at pandas/core/arrays/arrow/array.py:1986
Returns
-------
array of ints or int
If value is array-like, array of insertion points.
If value is scalar, a single integer.
See Also
--------
numpy.searchsorted : Similar method from NumPy.
Examples
--------
>>> arr = pd.array([1, 2, 3, 5], dtype="int64[pyarrow]")
>>> arr.searchsorted([4])
array([3])
"""
if self._hasna:
raise ValueError(
"searchsorted requires array to be sorted, which is impossible "
"with NAs present."
)
if isinstance(value, ExtensionArray):
value = value.astype(object)
# Base class searchsorted would cast to object, which is *much* slower.
dtype = None
if isinstance(self.dtype, ArrowDtype):
pa_dtype = self.dtype.pyarrow_dtype
if (
pa.types.is_timestamp(pa_dtype) or pa.types.is_duration(pa_dtype)
) and pa_dtype.unit == "ns":
# np.array[datetime/timedelta].searchsorted(datetime/timedelta)
# erroneously fails when numpy type resolution is nanoseconds
dtype = object
return self.to_numpy(dtype=dtype).searchsorted(value, side=side, sorter=sorter)
def take(View on GitHub (pinned to 71959b8cb9)
Solutions
- Drop or fill NAs before searchsorted: `arr = arr[~arr.isna()]` then ensure sorted order.
- Re-sort the array after cleaning NAs: `arr = arr.sort_values()`.
- If you must search in the presence of NAs, separate nulls out and search the non-null portion.
Example fix
// before s = pd.Series([1, None, 3], dtype="int64[pyarrow]") s.searchsorted(2) // after s = s.dropna().sort_values() s.searchsorted(2)
Defensive patterns
Strategy: validation
Validate before calling
def prepare_for_searchsorted(arr):
if arr.isna().any():
arr = arr[~arr.isna()]
return arr.sort_values() Type guard
def is_searchsortable(arr) -> bool:
return not bool(arr.isna().any()) Try / catch
try:
arr.searchsorted(v)
except ValueError as e:
if "requires array to be sorted" in str(e):
arr = arr[~arr.isna()].sort_values()
return arr.searchsorted(v)
raise Prevention
- Always dropna and sort before searchsorted.
- Add a precondition check `not s.isna().any()` in pipelines that depend on searchsorted.
- Treat nulls explicitly before binary-search style operations.
When it happens
Trigger: Calling `arr.searchsorted(v)` (or `Series.searchsorted`) on a pyarrow-backed array where `self._hasna` is True — i.e. the array contains any nulls/`pd.NA`.
Common situations: Calling searchsorted on a column that still has missing values, on data freshly loaded from CSV with NaNs not yet filled, or after a merge/join that introduced nulls.
Related errors
- searchsorted requires array to be sorted, which is impossibl
- Only np.ndarray, ExtensionArray, and Index objects are allow
- invalid na_position: {na_position}
- Cannot modify read-only array
- Length of 'value' does not match. Got ({len(value)}) expect
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/7fd9ea9b43c208a5.
Report an issue: GitHub.