-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
DOC: Add examples for MultiIndex.get_locs + cleanups #17675
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,8 +72,8 @@ class MultiIndex(Index): | |
Examples | ||
--------- | ||
A new ``MultiIndex`` is typically constructed using one of the helper | ||
methods :meth:`MultiIndex.from_arrays``, :meth:`MultiIndex.from_product`` | ||
and :meth:`MultiIndex.from_tuples``. For example (using ``.from_arrays``): | ||
methods :meth:`MultiIndex.from_arrays`, :meth:`MultiIndex.from_product` | ||
and :meth:`MultiIndex.from_tuples`. For example (using ``.from_arrays``): | ||
|
||
>>> arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']] | ||
>>> pd.MultiIndex.from_arrays(arrays, names=('number', 'color')) | ||
|
@@ -1985,13 +1985,9 @@ def get_loc(self, key, method=None): | |
Get location for a label or a tuple of labels as an integer, slice or | ||
boolean mask. | ||
|
||
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. I would put this Note in Notes. |
||
Note that the key cannot be a slice, list of same-level labels, | ||
a boolean mask, or a sequence of such. If you want that, use | ||
:meth:`get_locs` instead. | ||
|
||
Parameters | ||
---------- | ||
key : label or tuple of labels | ||
key : label or tuple of labels (one for each level) | ||
method : None | ||
|
||
Returns | ||
|
@@ -2003,17 +1999,24 @@ def get_loc(self, key, method=None): | |
Examples | ||
--------- | ||
>>> mi = pd.MultiIndex.from_arrays([list('abb'), list('def')]) | ||
|
||
>>> mi.get_loc('b') | ||
slice(1, 3, None) | ||
|
||
>>> mi.get_loc(('b', 'e')) | ||
1 | ||
|
||
Notes | ||
------ | ||
The key cannot be a slice, list of same-level labels, a boolean mask, | ||
or a sequence of such. If you want to use those, use :meth:`get_locs` | ||
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. get_locs -> MultiIndex.get_locs (I think, it would actually be interesting to know whether it works like this if it is a method of the same class. Did you build it locally and checked? But I think this will not work, because in the generated rst docstring pages, a 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. I've Changed it. It it possible to to build just the API docs or maybe a subgroup thereof? Working with doc is a but cumbersome, when building it takes so much time. 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. you pass the arg —�single api 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. Yes, it can indeed be very cumbersome. It's a very long time ago that I tried the If you think of other ways to make this easier, very welcome! (I once had a script to build only a single docstring, I could try to look that up, but not sure that such links then would work anyhow) |
||
instead. | ||
|
||
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. | ||
get_locs : Get location for a label/slice/list/mask or a sequence of | ||
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. get_locs -> MultiIndex.get_locs 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. Ok |
||
such. | ||
""" | ||
if method is not None: | ||
raise NotImplementedError('only the default get_loc method is ' | ||
|
@@ -2122,7 +2125,9 @@ def get_loc_level(self, key, level=0, drop_level=True): | |
|
||
See Also | ||
--------- | ||
MultiIndex.get_loc : Get location for a label or a tuple of labels. | ||
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 | ||
""" | ||
|
||
def maybe_droplevels(indexer, levels, drop_level): | ||
|
@@ -2332,14 +2337,14 @@ def convert_indexer(start, stop, step, indexer=indexer, labels=labels): | |
j = labels.searchsorted(loc, side='right') | ||
return slice(i, j) | ||
|
||
def get_locs(self, tup): | ||
def get_locs(self, seq): | ||
""" | ||
Get location for a given label/slice/list/mask or a sequence of such as | ||
a integer, slice or boolean mask. | ||
|
||
Parameters | ||
---------- | ||
key : label/slice/list/mask or a sequence of such. | ||
seq : label/slice/list/mask or a sequence of such. | ||
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. It might be good to explain how the 'sequence of such' should be interpreted (I should check the code myself to actually know it, but are the different elements of the list mapped to the different levels?) 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. maybe add here "(one for each level)" as you did in get_loc 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. I've made a new proposal. This is very difficult to explain in short sentences, I find. |
||
|
||
Returns | ||
------- | ||
|
@@ -2365,7 +2370,7 @@ def get_locs(self, tup): | |
""" | ||
|
||
# must be lexsorted to at least as many levels | ||
true_slices = [i for (i, s) in enumerate(is_true_slices(tup)) if s] | ||
true_slices = [i for (i, s) in enumerate(is_true_slices(seq)) if s] | ||
if true_slices and true_slices[-1] >= self.lexsort_depth: | ||
raise UnsortedIndexError('MultiIndex slicing requires the index ' | ||
'to be lexsorted: slicing on levels {0}, ' | ||
|
@@ -2398,7 +2403,7 @@ def _update_indexer(idxr, indexer=indexer): | |
return indexer | ||
return indexer & idxr | ||
|
||
for i, k in enumerate(tup): | ||
for i, k in enumerate(seq): | ||
|
||
if is_bool_indexer(k): | ||
# a boolean indexer, must be the same length! | ||
|
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.
does this property xref here? (CategoricalDtype is NOT in the main namespace)
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.
Ok, I've changed it to
api.types.CategoricalDtype
. I had misunderstood the location.Uh oh!
There was an error while loading. Please reload this page.
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.
Looking more into it, I actually think
CategoricalDtype
isn't shown at all in the generated API docs, so the reference will be not work in any case. Is this correct?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.
Yeah, the problem is not that 'pandas' is there (that is perfectly OK, and in this case I even prefer to show the full path for clarity, as
api
is not a commonly used submodule of pandas), the issue is that this page does not exist, so it cannot link to itThere 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.
I'll put
pandas.
back in then.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.
Yep. You can also add it to the api.rst pages so the link will work
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.
Change uploaded.
There seems to be something in the API.rst (line 649), but it mustn't work. @TomAugspurger has started looking into it, I'll wait till he figures it out.
If I get to know what I should add to API.rst, I can add it to this PR.
Uh oh!
There was an error while loading. Please reload this page.
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.
In api.rst it should be
pandas.api.type.CategoricalDtype
(missing the pandas).But the autoclass fit the best with the rest of the autosummary's, so I may change it. For now, I'd just leave your reference as
pandas.api.type.CategoricalDtype
, and I'll make sure they're valid before the release.