Skip to content

Commit 46159be

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

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

pandas/core/indexes/base.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -2441,10 +2441,22 @@ 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
24462445
2447-
@Appender(_index_shared_docs['get_loc'])
2446+
Examples
2447+
---------
2448+
>>> unique_index = pd.%(klass)s(list('abc'))
2449+
>>> unique_index.get_loc('b')
2450+
1
2451+
>>> monotonic_index = pd.%(klass)s(list('abbc'))
2452+
>>> monotonic_index.get_loc('b')
2453+
slice(1, 3, None)
2454+
>>> non_monotonic_index = pd.%(klass)s(list('abcb'))
2455+
>>> non_monotonic_index.get_loc('b')
2456+
array([False, True, False, True], dtype=bool)
2457+
"""
2458+
2459+
@Appender(_index_shared_docs['get_loc'] % _index_doc_kwargs)
24482460
def get_loc(self, key, method=None, tolerance=None):
24492461
if method is None:
24502462
if tolerance is not 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 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

+33
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,39 @@ 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 for requested label.
693+
694+
Parameters
695+
----------
696+
key : label
697+
method : {None, 'pad'/'ffill', 'backfill'/'bfill', 'nearest'}, optional
698+
* default: matches where the label is within an interval only.
699+
* the other methods are not currently implemented.
700+
701+
Returns
702+
-------
703+
loc : int if unique index, slice if monotonic index, else mask
704+
705+
Examples
706+
---------
707+
>>> i1, i2 = pd.Interval(0, 1), pd.Interval(1, 2)
708+
>>> index = pd.IntervalIndex.from_intervals([i1, i2])
709+
>>> index.get_loc(1)
710+
0
711+
712+
You can also get the location for a point inside an interval.
713+
714+
>>> index.get_loc(1.5)
715+
1
716+
717+
If a label is in several intervals, you get the locations of all the
718+
relevant intervals.
719+
720+
>>> i3 = pd.Interval(0, 2)
721+
>>> overlapping_index = pd.IntervalIndex.from_intervals([i2, i3])
722+
>>> overlapping_index.get_loc(1.5)
723+
array([0, 1], dtype=int64)
724+
"""
692725
self._check_method(method)
693726

694727
original_key = key

pandas/core/indexes/multi.py

+14
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,20 @@ 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 locations
19691983
"""
19701984
if method is not None:
19711985
raise NotImplementedError('only the default get_loc method is '

0 commit comments

Comments
 (0)