{"record":{"id":"f9d465f22ec115b2","repo":"pandas-dev/pandas","slug":"searchsorted-requires-array-to-be-sorted-which-is-f9d465","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":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_.py","lineNumber":1198,"sourceCode":"        \"\"\"\n\n        # GH#65837: avoid O(n) scan; NA confined to array ends in sorted data.\n        # When sorter is given, the sorted order is ndarray[sorter], so check\n        # the first/last positions via sorter instead of raw ndarray positions.\n        ndarray = self._ndarray\n        if len(ndarray):\n            if sorter is None:\n                has_na = libmissing.checknull(ndarray[0]) or libmissing.checknull(\n                    ndarray[-1]\n                )\n            else:\n                has_na = libmissing.checknull(\n                    ndarray[sorter[0]]\n                ) or libmissing.checknull(ndarray[sorter[-1]])\n        else:\n            has_na = False\n        if has_na:\n            raise ValueError(\n                \"searchsorted requires array to be sorted, which is impossible \"\n                \"with NAs present.\"\n            )\n        return super().searchsorted(value=value, side=side, sorter=sorter)\n\n    def _cmp_method(self, other, op):\n        from pandas.arrays import (\n            ArrowExtensionArray,\n            BooleanArray,\n        )\n\n        if (\n            isinstance(other, BaseStringArray)\n            and self.dtype.na_value is not libmissing.NA\n            and other.dtype.na_value is libmissing.NA\n        ):\n            # NA has priority of NaN semantics\n            return op(self.astype(other.dtype, copy=False), other)","sourceCodeStart":1180,"sourceCodeEnd":1216,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_.py#L1180-L1216","documentation":"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.","triggerScenarios":"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.","commonSituations":"Binary-search lookups on string data loaded from CSVs with missing fields; using searchsorted on an uncleaned column; sorted indexes that still carry nulls.","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."],"exampleFix":"// before\nstring_array.searchsorted('x')\n\n// after\nclean = string_array[~string_array.isna()]\nclean.searchsorted('x')","handlingStrategy":"validation","validationCode":"clean = string_array[~string_array.isna()]\nresult = clean.searchsorted('x')","typeGuard":"import pandas as pd\n\ndef has_no_na(arr) -> bool:\n    return not bool(pd.isna(arr).any())","tryCatchPattern":null,"preventionTips":["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."],"tags":["string-array","searchsorted","missing-values","sort"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}