{"record":{"id":"7fd9ea9b43c208a5","repo":"pandas-dev/pandas","slug":"searchsorted-requires-array-to-be-sorted-which-is","errorCode":null,"errorMessage":"searchsorted requires array to be sorted, which is impossible with NAs present.","messagePattern":"searchsorted requires array to be sorted, which is impossible with NAs present\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":1986,"sourceCode":"\n        Returns\n        -------\n        array of ints or int\n            If value is array-like, array of insertion points.\n            If value is scalar, a single integer.\n\n        See Also\n        --------\n        numpy.searchsorted : Similar method from NumPy.\n\n        Examples\n        --------\n        >>> arr = pd.array([1, 2, 3, 5], dtype=\"int64[pyarrow]\")\n        >>> arr.searchsorted([4])\n        array([3])\n        \"\"\"\n        if self._hasna:\n            raise ValueError(\n                \"searchsorted requires array to be sorted, which is impossible \"\n                \"with NAs present.\"\n            )\n        if isinstance(value, ExtensionArray):\n            value = value.astype(object)\n        # Base class searchsorted would cast to object, which is *much* slower.\n        dtype = None\n        if isinstance(self.dtype, ArrowDtype):\n            pa_dtype = self.dtype.pyarrow_dtype\n            if (\n                pa.types.is_timestamp(pa_dtype) or pa.types.is_duration(pa_dtype)\n            ) and pa_dtype.unit == \"ns\":\n                # np.array[datetime/timedelta].searchsorted(datetime/timedelta)\n                # erroneously fails when numpy type resolution is nanoseconds\n                dtype = object\n        return self.to_numpy(dtype=dtype).searchsorted(value, side=side, sorter=sorter)\n\n    def take(","sourceCodeStart":1968,"sourceCodeEnd":2004,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/arrow/array.py#L1968-L2004","documentation":"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.","triggerScenarios":"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`.","commonSituations":"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.","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."],"exampleFix":"// before\ns = pd.Series([1, None, 3], dtype=\"int64[pyarrow]\")\ns.searchsorted(2)\n\n// after\ns = s.dropna().sort_values()\ns.searchsorted(2)","handlingStrategy":"validation","validationCode":"def prepare_for_searchsorted(arr):\n    if arr.isna().any():\n        arr = arr[~arr.isna()]\n    return arr.sort_values()","typeGuard":"def is_searchsortable(arr) -> bool:\n    return not bool(arr.isna().any())","tryCatchPattern":"try:\n    arr.searchsorted(v)\nexcept ValueError as e:\n    if \"requires array to be sorted\" in str(e):\n        arr = arr[~arr.isna()].sort_values()\n        return arr.searchsorted(v)\n    raise","preventionTips":["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."],"tags":["arrow","searchsorted","na","sorting"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}