cudf.MultiIndex.get_loc#

MultiIndex.get_loc(key, method=None, tolerance=None)#

Get location for a label or a tuple of labels.

The location is returned as an integer/slice or boolean mask.

Parameters
keylabel or tuple of labels (one for each level)
methodNone
Returns
locint, slice object or boolean mask
  • If index is unique, search result is unique, return a single int.

  • If index is monotonic, index is returned as a slice object.

  • Otherwise, cudf attempts a best effort to convert the search result into a slice object, and will return a boolean mask if failed to do so. Notice this can deviate from Pandas behavior in some situations.

Examples

>>> import cudf
>>> mi = cudf.MultiIndex.from_tuples(
...     [('a', 'd'), ('b', 'e'), ('b', 'f')])
>>> mi.get_loc('b')
slice(1, 3, None)
>>> mi.get_loc(('b', 'e'))
1
>>> non_monotonic_non_unique_idx = cudf.MultiIndex.from_tuples(
...     [('c', 'd'), ('b', 'e'), ('a', 'f'), ('b', 'e')])
>>> non_monotonic_non_unique_idx.get_loc('b') # differ from pandas
slice(1, 4, 2)

Pandas Compatibility Note

MultiIndex.get_loc

The return types of this function may deviates from the method provided by Pandas. If the index is neither lexicographically sorted nor unique, a best effort attempt is made to coerce the found indices into a slice. For example:

>>> import pandas as pd
>>> import cudf
>>> x = pd.MultiIndex.from_tuples([
...     (2, 1, 1), (1, 2, 3), (1, 2, 1),
...     (1, 1, 1), (1, 1, 1), (2, 2, 1),
... ])
>>> x.get_loc(1)
array([False,  True,  True,  True,  True, False])
>>> cudf.from_pandas(x).get_loc(1)
slice(1, 5, 1)