Skip to content

Commit 8c9839f

Browse files
author
tp
committed
add examples to .get_loc methods
1 parent 138be88 commit 8c9839f

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

pandas/core/indexes/base.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -2421,7 +2421,7 @@ def _get_unique_index(self, dropna=False):
24212421
return self._shallow_copy(values)
24222422

24232423
_index_shared_docs['get_loc'] = """
2424-
Get integer location for requested label.
2424+
Get integer location, slice or boolean mask for requested label.
24252425
24262426
Parameters
24272427
----------
@@ -2441,8 +2441,20 @@ def _get_unique_index(self, dropna=False):
24412441
24422442
Returns
24432443
-------
2444-
loc : int if unique index, possibly slice or mask if not
2445-
"""
2444+
loc : int if unique index, slice if monotonic index, else mask
2445+
2446+
Examples
2447+
---------
2448+
>>> unique_index = pd.Index(list('abc'))
2449+
>>> unique_index.get_loc('b')
2450+
1
2451+
>>> monotonic_index = pd.Index(list('abbc'))
2452+
>>> monotonic_index.get_loc('b')
2453+
slice(1, 3, None)
2454+
>>> non_monotonic_index = pd.Index(list('abcb'))
2455+
>>> non_monotonic_index.get_loc('b')
2456+
array([False, True, False, True], dtype=bool)
2457+
"""
24462458

24472459
@Appender(_index_shared_docs['get_loc'])
24482460
def get_loc(self, key, method=None, tolerance=None):

pandas/core/indexes/category.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def _to_safe_for_reshape(self):
354354

355355
def get_loc(self, key, method=None):
356356
"""
357-
Get integer location for requested label
357+
Get integer location, slice or boolean mask for requested label.
358358
359359
Parameters
360360
----------
@@ -364,7 +364,19 @@ def get_loc(self, key, method=None):
364364
365365
Returns
366366
-------
367-
loc : int if unique index, possibly slice or mask if not
367+
loc : int if unique index, slice if monotonic index, else mask
368+
369+
Examples
370+
---------
371+
>>> unique_index = pd.CategoricalIndex(list('abc'))
372+
>>> unique_index.get_loc('b')
373+
1
374+
>>> monotonic_index = pd.CategoricalIndex(list('abbc'))
375+
>>> monotonic_index.get_loc('b')
376+
slice(1, 3, None)
377+
>>> non_monotonic_index = p.dCategoricalIndex(list('abcb'))
378+
>>> non_monotonic_index.get_loc('b')
379+
array([False, True, False, True], dtype=bool)
368380
"""
369381
codes = self.categories.get_loc(key)
370382
if (codes == -1):

pandas/core/indexes/interval.py

+34
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,40 @@ def _find_non_overlapping_monotonic_bounds(self, key):
689689
return start, stop
690690

691691
def get_loc(self, key, method=None):
692+
"""Get integer location, slice or boolean mask for requested label.
693+
694+
Parameters
695+
----------
696+
key : label
697+
method : {None}, optional
698+
* default: matches where the label is within an interval only.
699+
700+
Returns
701+
-------
702+
loc : int if unique index, slice if monotonic index, else mask
703+
704+
Examples
705+
---------
706+
>>> i1, i2 = pd.Interval(0, 1), pd.Interval(1, 2)
707+
>>> index = pd.IntervalIndex.from_intervals([i1, i2])
708+
>>> index.get_loc(1)
709+
0
710+
711+
You can also supply an interval or an location for a point inside an interval.
712+
713+
>>> index.get_loc(pd.Interval(0, 2))
714+
array([0, 1], dtype=int64)
715+
>>> index.get_loc(1.5)
716+
1
717+
718+
If a label is in several intervals, you get the locations of all the
719+
relevant intervals.
720+
721+
>>> i3 = pd.Interval(0, 2)
722+
>>> overlapping_index = pd.IntervalIndex.from_intervals([i2, i3])
723+
>>> overlapping_index.get_loc(1.5)
724+
array([0, 1], dtype=int64)
725+
"""
692726
self._check_method(method)
693727

694728
original_key = key

pandas/core/indexes/multi.py

+15
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,21 @@ def get_loc(self, key, method=None):
19661966
Returns
19671967
-------
19681968
loc : int, slice object or boolean mask
1969+
1970+
Examples
1971+
---------
1972+
>>> mi = pd.MultiIndex.from_arrays([list('abb'), list('def')])
1973+
>>> mi.get_loc('b')
1974+
slice(1, 3, None)
1975+
>>> mi.get_loc(('b', 'e'))
1976+
1
1977+
1978+
See also
1979+
--------
1980+
Index.get_loc : get_loc method for (single-level) index.
1981+
get_locs : Given a tuple of slices/lists/labels/boolean indexer to a
1982+
level-wise spec, produce an indexer to extract those
1983+
locations.
19691984
"""
19701985
if method is not None:
19711986
raise NotImplementedError('only the default get_loc method is '

0 commit comments

Comments
 (0)