-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: Add examples to MultiIndex.slice_locs + note that index.slice requires #17799
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
jorisvandenbossche
merged 4 commits into
pandas-dev:master
from
topper-123:MultiIndex.slice_locs
Oct 6, 2017
+46
−2
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1924,7 +1924,9 @@ def get_slice_bound(self, label, side, kind): | |
def slice_locs(self, start=None, end=None, step=None, kind=None): | ||
""" | ||
For an ordered MultiIndex, compute the slice locations for input | ||
labels. They can be tuples representing partial levels, e.g. for a | ||
labels. | ||
|
||
The input labels can be tuples representing partial levels, e.g. for a | ||
MultiIndex with 3 levels, you can pass a single value (corresponding to | ||
the first level), or a 1-, 2-, or 3-tuple. | ||
|
||
|
@@ -1944,7 +1946,32 @@ def slice_locs(self, start=None, end=None, step=None, kind=None): | |
|
||
Notes | ||
----- | ||
This function assumes that the data is sorted by the first level | ||
This method only works if the MultiIndex is properly lex-sorted. So, | ||
if only the first 2 levels of a 3-level MultiIndex are lexsorted, | ||
you can only pass two levels to ``.slice_locs``. | ||
|
||
Examples | ||
-------- | ||
mi = pd.MultiIndex.from_arrays([list('abbd'), list('deff')], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use
notation for the examples |
||
names=['A', 'B']) | ||
|
||
Get the slice locations from the beginning of 'b' in the first level | ||
until the end of the multiindex: | ||
|
||
mi.slice_locs(start='b') | ||
(1, 4) | ||
|
||
Like above, but stop at the end of 'b' in the first level and 'f' in | ||
the second level: | ||
|
||
mi.slice_locs(start='b', end=('b', 'f')) | ||
(1, 3) | ||
|
||
See Also | ||
-------- | ||
MultiIndex.get_loc : Get location for a label or a tuple of labels. | ||
MultiIndex.get_locs : Get location for a label/slice/list/mask or a | ||
sequence of such. | ||
""" | ||
# This function adds nothing to its parent implementation (the magic | ||
# happens in get_slice_bound method), but it adds meaningful doc. | ||
|
@@ -2015,6 +2042,8 @@ def get_loc(self, key, method=None): | |
See also | ||
-------- | ||
Index.get_loc : get_loc method for (single-level) index. | ||
MultiIndex.slice_locs : Get slice location given startlabel(s) and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. startlabel -> start label |
||
endlabel(s). | ||
MultiIndex.get_locs : Get location for a label/slice/list/mask or a | ||
sequence of such. | ||
""" | ||
|
@@ -2368,6 +2397,8 @@ def get_locs(self, seq): | |
See also | ||
-------- | ||
MultiIndex.get_loc : Get location for a label or a tuple of labels. | ||
MultiIndex.slice_locs : Get slice location given startlabel(s) and | ||
endlabel(s). | ||
""" | ||
|
||
# must be lexsorted to at least as many levels | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Index does not need to be ordered (it's only when there are duplicates that it has to be ordered I think). eg:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, thanks for catching that. I've reworded.