Skip to content

Commit cffb863

Browse files
committed
sortedarray in side multi.py implemented, testing pending
1 parent 9bfbe9e commit cffb863

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,5 @@ doc/source/savefig/
141141
# Pyodide/WASM related files #
142142
##############################
143143
/.pyodide-xbuildenv-*
144+
145+
*.ipynb

pandas/core/indexes/multi.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3778,6 +3778,60 @@ def _reorder_indexer(
37783778
ind = np.lexsort(keys)
37793779
return indexer[ind]
37803780

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+
37813835
def truncate(self, before=None, after=None) -> MultiIndex:
37823836
"""
37833837
Slice index between two labels / tuples, return new MultiIndex.
@@ -4337,3 +4391,6 @@ def cartesian_product(X: list[np.ndarray]) -> list[np.ndarray]:
43374391
)
43384392
for i, x in enumerate(X)
43394393
]
4394+
4395+
4396+

0 commit comments

Comments
 (0)