Skip to content

DOC: update the Index.get_level_values docstring #20210

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

Conversation

bkil-syslogng
Copy link
Contributor

Checklist for the pandas documentation sprint (ignore this if you are doing
an unrelated PR):

  • PR title is "DOC: update the docstring"
  • The validation script passes: scripts/validate_docstrings.py <your-function-or-method>
  • The PEP8 style check passes: git diff upstream/master -u -- "*.py" | flake8 --diff
  • The html version looks good: python doc/make.py --single <your-function-or-method>
  • It has been proofread on language by another sprint participant

Please include the output of the validation script below between the "```" ticks:

################################################################################
################## Docstring (pandas.Index.get_level_values)  ##################
################################################################################

Return an Index of values for requested level.

Return an Index of values for requested level, equal to the length
of the index.

Parameters
----------
level : int or str
    It is either the integer position of the level in the
    MultiIndex, or the `name` of the level.
    If given as an integer, it must be between 0 and the
    number of levels.

Returns
-------
values : Index
    `self`, as there is only one level in the Index.

See also
---------
pandas.MultiIndex.get_level_values : get values for a level of a
                                     MultiIndex

Notes
---------
For `Index`, level should be 0, since there are no multiple levels.

Examples
---------

Create an Index:

>>> idx = pd.Index(list('abc'))

Get level value by supplying level as integer:

>>> idx.get_level_values(0)
Index(['a', 'b', 'c'], dtype='object')

Create a MultiIndex:

>>> mi = pd.MultiIndex.from_arrays((list('abc'), list('def')))
>>> mi.names = ['level_1', 'level_2']

Get level values by supplying level as name:

>>> mi.get_level_values('level_2')
Index(['d', 'e', 'f'], dtype='object', name='level_2')

################################################################################
################################## Validation ##################################
################################################################################

Docstring for "pandas.Index.get_level_values" correct. :)

``level`` is either the integer position of the level in the
MultiIndex, or the name of the level.
It is either the integer position of the level in the
MultiIndex, or the `name` of the level.
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't think name has to be in backticks, since it isn't a parameter

Copy link
Contributor

Choose a reason for hiding this comment

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

Since MultiIndex has its own docstring, you probably don't have to reference it here.

It is either the integer position of the level in the
MultiIndex, or the `name` of the level.
If given as an integer, it must be between 0 and the
number of levels.
Copy link
Contributor

Choose a reason for hiding this comment

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

Number of levels - 1?

Examples
---------

Create an Index:
Copy link
Contributor

Choose a reason for hiding this comment

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

Probalby don't need this line. I'd remove it and add a line showing idx


Create a MultiIndex:

>>> mi = pd.MultiIndex.from_arrays((list('abc'), list('def')))
Copy link
Contributor

Choose a reason for hiding this comment

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

can pass names= to from_arrays

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although line wrapping would be needed and this case and it would still end up in two lines, which isn't as nice in my opinion. Although I'll change it as you've requested.

@@ -2788,26 +2788,55 @@ def set_value(self, arr, key, value):

def _get_level_values(self, level):
"""
Return an Index of values for requested level.

Return an Index of values for requested level, equal to the length
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add that this is mainly for compatibility with MultiIndex.

@TomAugspurger
Copy link
Contributor

Reading through this, it may make sense to say that this isn't very useful for non-MultiIndex indexes. A small example should be OK, but the most important bit is the See Also to MultiIndex.get_level_values.

Copy link
Contributor

@joaoavf joaoavf left a comment

Choose a reason for hiding this comment

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

Very good, I would just make it clear in the summary and extended summary that this method is specially useful to deal with MultiIndex.

@@ -2788,26 +2788,55 @@ def set_value(self, arr, key, value):

def _get_level_values(self, level):
"""
Return an Index of values for requested level.
Copy link
Contributor

Choose a reason for hiding this comment

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

Return an Index of values for requested level. --> Return an Index of values for requested level, specially useful for MultiIndex.

It is important to make it clear somehow that this is most useful when dealing with a MultiIndex.


Notes
---------
For `Index`, level should be 0, since there are no multiple levels.
Copy link
Contributor

Choose a reason for hiding this comment

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

no backticks

Create an Index:

>>> idx = pd.Index(list('abc'))

Copy link
Contributor

Choose a reason for hiding this comment

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

I wouldn't bother show this for an Index.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please elaborate what kind of change you would like to us to make. MultiIndex already mentions the other example. In our view, working on an Index is also useful to see for users somewhere, and this would be a proper place for it.

@bkil-syslogng bkil-syslogng force-pushed the updating_Index.get_level_values_docstring branch from 90e6b68 to fe2f525 Compare March 10, 2018 20:51
@bkil-syslogng
Copy link
Contributor Author

Most of the notes have been addressed and force pushed.

@@ -2788,26 +2788,45 @@ def set_value(self, arr, key, value):

def _get_level_values(self, level):
"""
Return an Index of values for requested level, specially useful for
Copy link
Contributor

Choose a reason for hiding this comment

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

need to be 1 line. you can remove after the comma.


Notes
---------
Copy link
Contributor

Choose a reason for hiding this comment

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

make the same length as Notes

@@ -2788,26 +2788,45 @@ def set_value(self, arr, key, value):

def _get_level_values(self, level):
"""
Return an Index of values for requested level, specially useful for
MultiIndex.

Return an Index of values for requested level, equal to the length
Copy link
Contributor

Choose a reason for hiding this comment

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

This is pretty repetitive with the summary. I think just the bit about MultiIndex is useful.

The is primarily useful to get an individual level of values from a MultiIndex,
but is provided on Index as well for compatability.

``level`` is either the integer position of the level in the
MultiIndex, or the name of the level.
It is either the integer position or the name of the level.
If given as an integer, it must be between 0 and the
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah this isn't quite right. Negative indices are accepted as well. Probably OK to just remove the second sentence, as to most Python programmers, "integer position" is clear enough.

>>> idx
Index(['a', 'b', 'c'], dtype='object')

Get level value by supplying level as integer:
Copy link
Contributor

Choose a reason for hiding this comment

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

backtick around `level`. Add "an" between "as integer"

@jorisvandenbossche
Copy link
Member

@bkil-syslogng Do you have time to update the PR based on the last comments?

@codecov
Copy link

codecov bot commented Jul 8, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@20ca108). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #20210   +/-   ##
=========================================
  Coverage          ?   91.72%           
=========================================
  Files             ?      150           
  Lines             ?    49162           
  Branches          ?        0           
=========================================
  Hits              ?    45096           
  Misses            ?     4066           
  Partials          ?        0
Flag Coverage Δ
#multiple 90.11% <100%> (?)
#single 41.86% <56.25%> (?)
Impacted Files Coverage Δ
pandas/core/indexes/base.py 96.66% <100%> (ø)

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 20ca108...6aa8b77. Read the comment docs.

@jorisvandenbossche
Copy link
Member

Rebased and added final fixes.

@bkil-syslogng Thanks!

@jorisvandenbossche jorisvandenbossche added this to the 0.24.0 milestone Jul 8, 2018
@jorisvandenbossche jorisvandenbossche merged commit 44c08d4 into pandas-dev:master Jul 8, 2018
Sup3rGeo pushed a commit to Sup3rGeo/pandas that referenced this pull request Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants