Skip to content

ENH: Implemented MultiIndex.searchsorted method ( GH14833) #61435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ensured typing hints consistency, closes issue 14833
  • Loading branch information
GSAUC3 committed May 30, 2025
commit 73e308ba3cde56be37348ea6b79f4dc8a33f17e5
30 changes: 17 additions & 13 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3841,14 +3841,16 @@ def searchsorted(
array([1])
"""

if not value:
raise ValueError("searchsorted requires a non-empty value")

if not isinstance(value, (tuple, list)):
raise TypeError("value must be a tuple or list")

if isinstance(value, tuple):
value = [value]
elif isinstance(value, (list, np.ndarray, ExtensionArray)):
if len(value) == 0:
raise ValueError("searchsorted requires a non-empty sequence")
else:
raise TypeError(
"value must be a tuple (scalar key), or a list/numpy"
"array/ExtensionArray of tuples"
)

if side not in ["left", "right"]:
raise ValueError("side must be either 'left' or 'right'")
Expand All @@ -3861,22 +3863,24 @@ def searchsorted(
val = i if side == "left" else i + 1
result.append(np.intp(val))
else:
dtype = np.dtype(
[
(f"level_{i}", np.asarray(level).dtype)
for i, level in enumerate(self.levels)
]
)
fields = []
for i, level in enumerate(self.levels):
level_dtype = level.dtype
if isinstance(level_dtype, ExtensionDtype):
fields.append((f"level_{i}", object))
else:
fields.append((f"level_{i}", level_dtype))
dtype = np.dtype(fields)

val_array = np.array([v], dtype=dtype)

pos = np.searchsorted(
np.asarray(self.values, dtype=dtype),
val_array,
side=side,
sorter=sorter,
)
result.append(np.intp(pos[0]))

if len(result) == 1:
return result[0]
return np.array(result, dtype=np.intp)
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,15 @@ def test_searchsorted_list(mi, values, side, expected):
"value, side, error_type, match",
[
(("a", 1), "middle", ValueError, "side must be either 'left' or 'right'"),
("a", "left", TypeError, "value must be a tuple or list"),
(
"a",
"left",
TypeError,
re.escape(
"value must be a tuple (scalar key), or a list/numpy"
"array/ExtensionArray of tuples"
),
),
],
ids=["invalid-side", "invalid-value-type"],
)
Expand Down
Loading