Skip to content

DOC: Add examples to .get_loc methods #17563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2421,7 +2421,7 @@ def _get_unique_index(self, dropna=False):
return self._shallow_copy(values)

_index_shared_docs['get_loc'] = """
Get integer location for requested label.
Get integer location, slice or boolean mask for requested label.

Parameters
----------
Expand All @@ -2441,8 +2441,22 @@ def _get_unique_index(self, dropna=False):

Returns
-------
loc : int if unique index, possibly slice or mask if not
"""
loc : int if unique index, slice if monotonic index, else mask

Examples
---------
>>> unique_index = pd.Index(list('abc'))
>>> unique_index.get_loc('b')
1

>>> monotonic_index = pd.Index(list('abbc'))
>>> monotonic_index.get_loc('b')
slice(1, 3, None)

>>> non_monotonic_index = pd.Index(list('abcb'))
>>> non_monotonic_index.get_loc('b')
array([False, True, False, True], dtype=bool)
"""

@Appender(_index_shared_docs['get_loc'])
def get_loc(self, key, method=None, tolerance=None):
Expand Down
18 changes: 16 additions & 2 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def _to_safe_for_reshape(self):

def get_loc(self, key, method=None):
"""
Get integer location for requested label
Get integer location, slice or boolean mask for requested label.

Parameters
----------
Expand All @@ -364,7 +364,21 @@ def get_loc(self, key, method=None):

Returns
-------
loc : int if unique index, possibly slice or mask if not
loc : int if unique index, slice if monotonic index, else mask

Examples
---------
>>> unique_index = pd.CategoricalIndex(list('abc'))
>>> unique_index.get_loc('b')
1

>>> monotonic_index = pd.CategoricalIndex(list('abbc'))
>>> monotonic_index.get_loc('b')
slice(1, 3, None)

>>> non_monotonic_index = p.dCategoricalIndex(list('abcb'))
>>> non_monotonic_index.get_loc('b')
array([False, True, False, True], dtype=bool)
"""
codes = self.categories.get_loc(key)
if (codes == -1):
Expand Down
35 changes: 35 additions & 0 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,41 @@ def _find_non_overlapping_monotonic_bounds(self, key):
return start, stop

def get_loc(self, key, method=None):
"""Get integer location, slice or boolean mask for requested label.

Parameters
----------
key : label
method : {None}, optional
* default: matches where the label is within an interval only.

Returns
-------
loc : int if unique index, slice if monotonic index, else mask

Examples
---------
>>> i1, i2 = pd.Interval(0, 1), pd.Interval(1, 2)
>>> index = pd.IntervalIndex.from_intervals([i1, i2])
>>> index.get_loc(1)
0

You can also supply an interval or an location for a point inside an
interval.

>>> index.get_loc(pd.Interval(0, 2))
array([0, 1], dtype=int64)
>>> index.get_loc(1.5)
1

If a label is in several intervals, you get the locations of all the
relevant intervals.

>>> i3 = pd.Interval(0, 2)
>>> overlapping_index = pd.IntervalIndex.from_intervals([i2, i3])
>>> overlapping_index.get_loc(1.5)
array([0, 1], dtype=int64)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are discussing to change this behaviour, but the above is still correct on master. However, I would change the example to already add get_loc(Interval(..)) cases (the case we intent to keep working, the scalar numerical value might raise in the future). So that way we can just remove those case that will not be valid anymore, but keep the exact match for actual Interval objects examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an interval example

"""
self._check_method(method)

original_key = key
Expand Down
15 changes: 15 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,21 @@ def get_loc(self, key, method=None):
Returns
-------
loc : int, slice object or boolean mask

Examples
---------
>>> mi = pd.MultiIndex.from_arrays([list('abb'), list('def')])
>>> mi.get_loc('b')
slice(1, 3, None)
>>> mi.get_loc(('b', 'e'))
1

See also
--------
Index.get_loc : get_loc method for (single-level) index.
get_locs : Given a tuple of slices/lists/labels/boolean indexer to a
level-wise spec, produce an indexer to extract those
locations.
"""
if method is not None:
raise NotImplementedError('only the default get_loc method is '
Expand Down