MCPcopy
hub / github.com/pandas-dev/pandas / get_loc

Method get_loc

pandas/core/indexes/multi.py:3460–3586  ·  view source on GitHub ↗

Get location for a label or a tuple of labels. The location is returned \ as an integer/slice or boolean mask. This method returns the integer location, slice object, or boolean mask corresponding to the specified key, which can be a single label or a tuple

(self, key)

Source from the content-addressed store, hash-verified

3458 return level_index.get_loc(key)
3459
3460 def get_loc(self, key):
3461 """
3462 Get location for a label or a tuple of labels. The location is returned \
3463 as an integer/slice or boolean mask.
3464
3465 This method returns the integer location, slice object, or boolean mask
3466 corresponding to the specified key, which can be a single label or a tuple
3467 of labels. The key represents a position in the MultiIndex, and the location
3468 indicates where the key is found within the index.
3469
3470 Parameters
3471 ----------
3472 key : label or tuple of labels (one for each level)
3473 A label or tuple of labels that correspond to the levels of the MultiIndex.
3474 The key must match the structure of the MultiIndex.
3475
3476 Returns
3477 -------
3478 int, slice object or boolean mask
3479 If the key is past the lexsort depth, the return may be a
3480 boolean mask array, otherwise it is always a slice or int.
3481
3482 See Also
3483 --------
3484 Index.get_loc : The get_loc method for (single-level) index.
3485 MultiIndex.slice_locs : Get slice location given start label(s) and
3486 end label(s).
3487 MultiIndex.get_locs : Get location for a label/slice/list/mask or a
3488 sequence of such.
3489
3490 Notes
3491 -----
3492 The key cannot be a slice, list of same-level labels, a boolean mask,
3493 or a sequence of such. If you want to use those, use
3494 :meth:`MultiIndex.get_locs` instead.
3495
3496 Examples
3497 --------
3498 >>> mi = pd.MultiIndex.from_arrays([list("abb"), list("def")])
3499
3500 >>> mi.get_loc("b")
3501 slice(1, 3, None)
3502
3503 >>> mi.get_loc(("b", "e"))
3504 1
3505 """
3506 self._check_indexing_error(key)
3507
3508 def _maybe_to_slice(loc):
3509 """convert integer indexer to boolean mask or slice if possible"""
3510 if not isinstance(loc, np.ndarray) or loc.dtype != np.intp:
3511 return loc
3512
3513 loc = lib.maybe_indices_to_slice(loc, len(self))
3514 if isinstance(loc, slice):
3515 return loc
3516
3517 mask = np.empty(len(self), dtype="bool")

Callers 9

__contains__Method · 0.95
dropMethod · 0.95
_get_loc_levelMethod · 0.95
test_get_locMethod · 0.95
_get_level_indexerMethod · 0.45
insertMethod · 0.45

Calls 8

_check_indexing_errorMethod · 0.95
_get_level_indexerMethod · 0.95
get_loc_levelMethod · 0.95
slice_locsMethod · 0.95
get_optionFunction · 0.90
find_stack_levelFunction · 0.90
allMethod · 0.45

Tested by 3

test_get_locMethod · 0.76