diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 3cccb65503378..f5328ce9e6317 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -2,6 +2,7 @@ # pylint: disable=E1101,E1103,W0232 import datetime import warnings +from functools import reduce from sys import getsizeof import numpy as np @@ -2899,6 +2900,13 @@ def isin(self, values, level=None): else: return np.lib.arraysetops.in1d(labs, sought_labels) + def searchsorted(self, arr): + dtype = [l.dtype.descr for l in self.levels] + dtype = reduce(lambda x, y: x + y, dtype, []) + arr = np.asarray(arr, dtype=dtype) + values = self.values.astype(dtype) + return values.searchsorted(arr) + MultiIndex._add_numeric_methods_disabled() MultiIndex._add_numeric_methods_add_sub_disabled() diff --git a/pandas/tests/indexes/multi/test_indexing.py b/pandas/tests/indexes/multi/test_indexing.py index 9ec11f1f42b9a..abe2604b5da71 100644 --- a/pandas/tests/indexes/multi/test_indexing.py +++ b/pandas/tests/indexes/multi/test_indexing.py @@ -346,3 +346,8 @@ def test_get_indexer_categorical_time(): Categorical(date_range("2012-01-01", periods=3, freq='H'))]) result = midx.get_indexer(midx) tm.assert_numpy_array_equal(result, np.arange(9, dtype=np.intp)) + + +def test_searchsorted(): + i = MultiIndex.from_tuples([('a', 0), ('b', 1)]).searchsorted(('b', 0)) + assert i == 1