MCPcopy
hub / github.com/pandas-dev/pandas / searchsorted

Function searchsorted

pandas/core/algorithms.py:1229–1310  ·  view source on GitHub ↗

Find indices where elements should be inserted to maintain order. Find the indices into a sorted array `arr` (a) such that, if the corresponding elements in `value` were inserted before the indices, the order of `arr` would be preserved. Assuming that `arr` is sorted: ===

(
    arr: ArrayLike,
    value: NumpyValueArrayLike | ExtensionArray,
    side: Literal["left", "right"] = "left",
    sorter: NumpySorter | None = None,
)

Source from the content-addressed store, hash-verified

1227
1228
1229def searchsorted(
1230 arr: ArrayLike,
1231 value: NumpyValueArrayLike | ExtensionArray,
1232 side: Literal["left", "right"] = "left",
1233 sorter: NumpySorter | None = None,
1234) -> npt.NDArray[np.intp] | np.intp:
1235 """
1236 Find indices where elements should be inserted to maintain order.
1237
1238 Find the indices into a sorted array `arr` (a) such that, if the
1239 corresponding elements in `value` were inserted before the indices,
1240 the order of `arr` would be preserved.
1241
1242 Assuming that `arr` is sorted:
1243
1244 ====== ================================
1245 `side` returned index `i` satisfies
1246 ====== ================================
1247 left ``arr[i-1] < value <= self[i]``
1248 right ``arr[i-1] <= value < self[i]``
1249 ====== ================================
1250
1251 Parameters
1252 ----------
1253 arr: np.ndarray, ExtensionArray, Series
1254 Input array. If `sorter` is None, then it must be sorted in
1255 ascending order, otherwise `sorter` must be an array of indices
1256 that sort it.
1257 value : array-like or scalar
1258 Values to insert into `arr`.
1259 side : {'left', 'right'}, optional
1260 If 'left', the index of the first suitable location found is given.
1261 If 'right', return the last such index. If there is no suitable
1262 index, return either 0 or N (where N is the length of `self`).
1263 sorter : 1-D array-like, optional
1264 Optional array of integer indices that sort array a into ascending
1265 order. They are typically the result of argsort.
1266
1267 Returns
1268 -------
1269 array of ints or int
1270 If value is array-like, array of insertion points.
1271 If value is scalar, a single integer.
1272
1273 See Also
1274 --------
1275 numpy.searchsorted : Similar method from NumPy.
1276 """
1277 if sorter is not None:
1278 sorter = ensure_platform_int(sorter)
1279
1280 if (
1281 isinstance(arr, np.ndarray)
1282 and arr.dtype.kind in "iu"
1283 and (is_integer(value) or is_integer_dtype(value))
1284 ):
1285 # if `arr` and `value` have different dtypes, `arr` would be
1286 # recast by numpy, causing a slow search.

Callers

nothing calls this directly

Calls 6

is_integer_dtypeFunction · 0.90
arrayMethod · 0.45
allMethod · 0.45
typeMethod · 0.45
searchsortedMethod · 0.45

Tested by

no test coverage detected