@@ -3778,6 +3778,60 @@ def _reorder_indexer(
3778
3778
ind = np .lexsort (keys )
3779
3779
return indexer [ind ]
3780
3780
3781
+
3782
+ def searchsorted (
3783
+ self ,
3784
+ value : tuple [Hashable , ...],
3785
+ side : Literal ["left" , "right" ] = "left" ,
3786
+ sorter : npt .NDArray [np .intp ] | None = None ,
3787
+ ) -> npt .NDArray [np .intp ]:
3788
+ """
3789
+ Find the indices where elements should be inserted to maintain order.
3790
+
3791
+ Parameters
3792
+ ----------
3793
+ value : tuple
3794
+ The value(s) to search for in the MultiIndex.
3795
+ side : {'left', 'right'}, default 'left'
3796
+ If 'left', the index of the first suitable location found is given.
3797
+ If 'right', return the last such index. Note that if `value` is
3798
+ already present in the MultiIndex, the results will be different.
3799
+ sorter : 1-D array-like, optional
3800
+ Optional array of integer indices that sort the MultiIndex.
3801
+
3802
+ Returns
3803
+ -------
3804
+ numpy.ndarray
3805
+ Array of insertion points.
3806
+
3807
+ See Also
3808
+ --------
3809
+ Index.searchsorted : Search for insertion point in a 1-D index.
3810
+
3811
+ Examples
3812
+ --------
3813
+ >>> mi = pd.MultiIndex.from_arrays([["a", "b", "c"], ["x", "y", "z"]])
3814
+ >>> mi.searchsorted(("b", "y"))
3815
+ 1
3816
+ """
3817
+ if isinstance (value , tuple ):
3818
+ value = list (value )
3819
+
3820
+ if side not in ["left" , "right" ]:
3821
+ raise ValueError ("side must be either 'left' or 'right'" )
3822
+
3823
+ if not value :
3824
+ raise ValueError ("searchsorted requires a non-empty value" )
3825
+
3826
+
3827
+
3828
+ dtype = np .dtype ([(f"level_{ i } " , level .dtype ) for i ,level in enumerate (self .levels )])
3829
+
3830
+ val = np .asarray (value , dtype = dtype )
3831
+
3832
+ return np .searchsorted (self .values .astype (dtype ),val , side = side , sorter = sorter )
3833
+
3834
+
3781
3835
def truncate (self , before = None , after = None ) -> MultiIndex :
3782
3836
"""
3783
3837
Slice index between two labels / tuples, return new MultiIndex.
@@ -4337,3 +4391,6 @@ def cartesian_product(X: list[np.ndarray]) -> list[np.ndarray]:
4337
4391
)
4338
4392
for i , x in enumerate (X )
4339
4393
]
4394
+
4395
+
4396
+
0 commit comments