| 
19 | 19 |  ensure_float64, ensure_int64, ensure_object, ensure_platform_int,  | 
20 | 20 |  ensure_uint64, is_array_like, is_bool_dtype, is_categorical_dtype,  | 
21 | 21 |  is_complex_dtype, is_datetime64_any_dtype, is_datetime64tz_dtype,  | 
22 |  | - is_datetimelike, is_extension_array_dtype, is_float_dtype,  | 
 | 22 | + is_datetimelike, is_extension_array_dtype, is_float_dtype, is_integer,  | 
23 | 23 |  is_integer_dtype, is_interval_dtype, is_list_like, is_numeric_dtype,  | 
24 | 24 |  is_object_dtype, is_period_dtype, is_scalar, is_signed_integer_dtype,  | 
25 | 25 |  is_sparse, is_timedelta64_dtype, is_unsigned_integer_dtype,  | 
@@ -1724,6 +1724,88 @@ def func(arr, indexer, out, fill_value=np.nan):  | 
1724 | 1724 |  return out  | 
1725 | 1725 | 
 
  | 
1726 | 1726 | 
 
  | 
 | 1727 | +# ---- #  | 
 | 1728 | +# searchsorted #  | 
 | 1729 | +# ---- #  | 
 | 1730 | + | 
 | 1731 | +def searchsorted(arr, value, side="left", sorter=None):  | 
 | 1732 | + """  | 
 | 1733 | + Find indices where elements should be inserted to maintain order.  | 
 | 1734 | +
  | 
 | 1735 | + .. versionadded:: 0.25.0  | 
 | 1736 | +
  | 
 | 1737 | + Find the indices into a sorted array `self` (a) such that, if the  | 
 | 1738 | + corresponding elements in `value` were inserted before the indices,  | 
 | 1739 | + the order of `self` would be preserved.  | 
 | 1740 | +
  | 
 | 1741 | + Assuming that `self` is sorted:  | 
 | 1742 | +
  | 
 | 1743 | + ====== ================================  | 
 | 1744 | + `side` returned index `i` satisfies  | 
 | 1745 | + ====== ================================  | 
 | 1746 | + left ``self[i-1] < value <= self[i]``  | 
 | 1747 | + right ``self[i-1] <= value < self[i]``  | 
 | 1748 | + ====== ================================  | 
 | 1749 | +
  | 
 | 1750 | + Parameters  | 
 | 1751 | + ----------  | 
 | 1752 | + arr: numpy.array or ExtensionArray  | 
 | 1753 | + array to search in. Cannot be Index, Series or PandasArray, as that  | 
 | 1754 | + would cause a RecursionError.  | 
 | 1755 | + value : array_like  | 
 | 1756 | + Values to insert into `arr`.  | 
 | 1757 | + side : {'left', 'right'}, optional  | 
 | 1758 | + If 'left', the index of the first suitable location found is given.  | 
 | 1759 | + If 'right', return the last such index. If there is no suitable  | 
 | 1760 | + index, return either 0 or N (where N is the length of `self`).  | 
 | 1761 | + sorter : 1-D array_like, optional  | 
 | 1762 | + Optional array of integer indices that sort array a into ascending  | 
 | 1763 | + order. They are typically the result of argsort.  | 
 | 1764 | +
  | 
 | 1765 | + Returns  | 
 | 1766 | + -------  | 
 | 1767 | + array of ints  | 
 | 1768 | + Array of insertion points with the same shape as `value`.  | 
 | 1769 | +
  | 
 | 1770 | + See Also  | 
 | 1771 | + --------  | 
 | 1772 | + numpy.searchsorted : Similar method from NumPy.  | 
 | 1773 | + """  | 
 | 1774 | + if sorter is not None:  | 
 | 1775 | + sorter = ensure_platform_int(sorter)  | 
 | 1776 | + | 
 | 1777 | + if is_integer_dtype(arr) and (  | 
 | 1778 | + is_integer(value) or is_integer_dtype(value)):  | 
 | 1779 | + from .arrays.array_ import array  | 
 | 1780 | + # if `arr` and `value` have different dtypes, `arr` would be  | 
 | 1781 | + # recast by numpy, causing a slow search.  | 
 | 1782 | + # Before searching below, we therefore try to give `value` the  | 
 | 1783 | + # same dtype as `arr`, while guarding against integer overflows.  | 
 | 1784 | + iinfo = np.iinfo(arr.dtype.type)  | 
 | 1785 | + value_arr = np.array([value]) if is_scalar(value) else np.array(value)  | 
 | 1786 | + if (value_arr >= iinfo.min).all() and (value_arr <= iinfo.max).all():  | 
 | 1787 | + # value within bounds, so no overflow, so can convert value dtype  | 
 | 1788 | + # to dtype of arr  | 
 | 1789 | + dtype = arr.dtype  | 
 | 1790 | + else:  | 
 | 1791 | + dtype = value_arr.dtype  | 
 | 1792 | + | 
 | 1793 | + if is_scalar(value):  | 
 | 1794 | + value = dtype.type(value)  | 
 | 1795 | + else:  | 
 | 1796 | + value = array(value, dtype=dtype)  | 
 | 1797 | + elif not (is_object_dtype(arr) or is_numeric_dtype(arr) or  | 
 | 1798 | + is_categorical_dtype(arr)):  | 
 | 1799 | + from pandas.core.series import Series  | 
 | 1800 | + # E.g. if `arr` is an array with dtype='datetime64[ns]'  | 
 | 1801 | + # and `value` is a pd.Timestamp, we may need to convert value  | 
 | 1802 | + value_ser = Series(value)._values  | 
 | 1803 | + value = value_ser[0] if is_scalar(value) else value_ser  | 
 | 1804 | + | 
 | 1805 | + result = arr.searchsorted(value, side=side, sorter=sorter)  | 
 | 1806 | + return result  | 
 | 1807 | + | 
 | 1808 | + | 
1727 | 1809 | # ---- #  | 
1728 | 1810 | # diff #  | 
1729 | 1811 | # ---- #  | 
 | 
0 commit comments