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

Conversation

topper-123
Copy link
Contributor

  • [ x] passes git diff upstream/master -u -- "*.py" | flake8 --diff

This pull request adds some examples to .get_loc doc string of pd.Index, pd.CategoricalIndex, pd.MultiIndex & pd.IntervalIndex. Also clarifies the return value in some cases.

I previously proposed pull request #17380, but the commit history there became a mess. I've deleted that pull request and added this instead.

@topper-123 topper-123 force-pushed the get_loc_examples branch 2 times, most recently from 944aa9b to 46159be Compare September 17, 2017 16:29
@codecov
Copy link

codecov bot commented Sep 17, 2017

Codecov Report

Merging #17563 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17563      +/-   ##
==========================================
- Coverage   91.25%   91.23%   -0.02%     
==========================================
  Files         163      163              
  Lines       49617    49617              
==========================================
- Hits        45277    45268       -9     
- Misses       4340     4349       +9
Flag Coverage Δ
#multiple 89.02% <100%> (ø) ⬆️
#single 40.19% <100%> (-0.07%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/multi.py 96.9% <ø> (ø) ⬆️
pandas/core/indexes/interval.py 93.57% <ø> (ø) ⬆️
pandas/core/indexes/category.py 98.55% <ø> (ø) ⬆️
pandas/core/indexes/base.py 96.28% <100%> (ø) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.77% <0%> (-0.1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 138be88...46159be. Read the comment docs.

@codecov
Copy link

codecov bot commented Sep 17, 2017

Codecov Report

Merging #17563 into master will decrease coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17563      +/-   ##
==========================================
- Coverage   91.25%   91.23%   -0.02%     
==========================================
  Files         163      163              
  Lines       49617    49617              
==========================================
- Hits        45277    45268       -9     
- Misses       4340     4349       +9
Flag Coverage Δ
#multiple 89.02% <100%> (ø) ⬆️
#single 40.19% <100%> (-0.07%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/interval.py 93.57% <ø> (ø) ⬆️
pandas/core/indexes/multi.py 96.9% <ø> (ø) ⬆️
pandas/core/indexes/category.py 98.55% <ø> (ø) ⬆️
pandas/core/indexes/base.py 96.28% <100%> (ø) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.77% <0%> (-0.1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 138be88...46159be. Read the comment docs.

@codecov
Copy link

codecov bot commented Sep 17, 2017

Codecov Report

Merging #17563 into master will decrease coverage by 0.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #17563      +/-   ##
==========================================
- Coverage   91.19%   91.18%   -0.02%     
==========================================
  Files         163      163              
  Lines       49626    49626              
==========================================
- Hits        45258    45249       -9     
- Misses       4368     4377       +9
Flag Coverage Δ
#multiple 88.96% <ø> (ø) ⬆️
#single 40.19% <ø> (-0.07%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexes/category.py 98.55% <ø> (ø) ⬆️
pandas/core/indexes/base.py 96.28% <ø> (ø) ⬆️
pandas/core/indexes/multi.py 96.9% <ø> (ø) ⬆️
pandas/core/indexes/interval.py 93.57% <ø> (ø) ⬆️
pandas/io/gbq.py 25% <0%> (-58.34%) ⬇️
pandas/core/frame.py 97.77% <0%> (-0.1%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b59f107...28c9c89. Read the comment docs.

@jreback jreback added Docs Indexing Related to indexing on series/frames, not to indexes themselves labels Sep 17, 2017
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Thanks! added some comments

@Appender(_index_shared_docs['get_loc'])
Examples
---------
>>> unique_index = pd.%(klass)s(list('abc'))
Copy link
Member

Choose a reason for hiding this comment

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

I don't think 'klass' substitution is needed for the example, just show it with base Index, logic of behaviour should be the same for all types (for other types of Index subclasses the 'abc' values might not be valid anyhow, and you get a 'wrong' example)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

Parameters
----------
key : label
method : {None, 'pad'/'ffill', 'backfill'/'bfill', 'nearest'}, optional
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't tlist the other methods, as you say they are not implemented (like you did in the categorical docstring)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

>>> 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

--------
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
Copy link
Member

Choose a reason for hiding this comment

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

extra level of indentation here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@@ -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 for requested label.
Copy link
Member

Choose a reason for hiding this comment

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

I actually like the "Get integer location, slice or boolean mask for requested label" from the multi-index docstring, as this seems to be more complete description?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, Will change

>>> monotonic_index = pd.%(klass)s(list('abbc'))
>>> monotonic_index.get_loc('b')
slice(1, 3, None)
>>> non_monotonic_index = pd.%(klass)s(list('abcb'))
Copy link
Contributor

Choose a reason for hiding this comment

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

put a blank line in-between cases

--------
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
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need to indent this (to the :)

@jreback
Copy link
Contributor

jreback commented Sep 18, 2017

I think you still need to use the @Appender of the shared docs, then add the examples

@pep8speaks
Copy link

pep8speaks commented Sep 18, 2017

Hello @topper-123! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on September 20, 2017 at 14:58 Hours UTC

@topper-123
Copy link
Contributor Author

topper-123 commented Sep 18, 2017

I've made various changes.The @appender is still there, though the examples are in it as well.

The .get_loc methods are subtly differerent for the differerent index types, so I think it will be difficult to get real doc reuse, though.

EDIT: Are you sure it's ok to force-push changes to the same commit? To me it seems that it makes reviewting more difficult, as the connection between code and comments seems to get lost.

@topper-123 topper-123 force-pushed the get_loc_examples branch 2 times, most recently from 0c9094d to 0ab5488 Compare September 18, 2017 13:12
@topper-123
Copy link
Contributor Author

The error is at pandas/tests/io/parser/test_network.py:74. with error message: HTTPError: HTTP Error 504: Gateway Time-out.

So the error seems to be unrelated to my pull request...

@jreback
Copy link
Contributor

jreback commented Sep 20, 2017

EDIT: Are you sure it's ok to force-push changes to the same commit? To me it seems that it makes reviewting more difficult, as the connection between code and comments seems to get lost.

if commits are logical / organized and have good messages, yes. however most commits I see should just be combined into 1 and promote even more confusion. rebasing is also much easier with fewer commits. so force pushing is fine as its your own fork.

@jreback
Copy link
Contributor

jreback commented Sep 20, 2017

looks fine. can you rebase and push again to force ci to run.

@jreback jreback added this to the 0.21.0 milestone Sep 20, 2017
@jorisvandenbossche
Copy link
Member

EDIT: Are you sure it's ok to force-push changes to the same commit? To me it seems that it makes reviewting more difficult, as the connection between code and comments seems to get lost.

For that reason in general I prefer to add commits instead of rebasing/force push, but to clean up messed up github state, it is perfectly fine (as long as it is in a branch).

@topper-123
Copy link
Contributor Author

Green @jreback

@jorisvandenbossche jorisvandenbossche merged commit fedf922 into pandas-dev:master Sep 20, 2017
@jorisvandenbossche
Copy link
Member

@topper-123 Thanks!

@topper-123 topper-123 deleted the get_loc_examples branch October 9, 2017 21:00
alanbato pushed a commit to alanbato/pandas that referenced this pull request Nov 10, 2017
No-Stream pushed a commit to No-Stream/pandas that referenced this pull request Nov 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants