Skip to content
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

DOC: Improving docstring of take method #16948

Conversation

matagus
Copy link
Contributor

@matagus matagus commented Jul 15, 2017

  • closes #xxxx
  • tests added / passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

@pep8speaks
Copy link

pep8speaks commented Jul 15, 2017

Hello @matagus! Thanks for updating the PR.

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

Comment last updated on August 21, 2017 at 08:25 Hours UTC

@jreback jreback added the Docs label Jul 15, 2017
@@ -1939,7 +1939,8 @@ def __delitem__(self, key):

def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
"""
Analogous to ndarray.take
Return an object formed from the elements in the given indices along an
Copy link
Member

Choose a reason for hiding this comment

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

We generally try to fit this description on one line.

Copy link
Member

Choose a reason for hiding this comment

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

Done.

@@ -1948,6 +1949,44 @@ def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
convert : translate neg to pos indices (default)
is_copy : mark the returned frame as a copy

Examples
--------
>>> import numpy as np
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need the imports here

Copy link
Member

Choose a reason for hiding this comment

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

Done.

@@ -1939,7 +1939,8 @@ def __delitem__(self, key):

def take(self, indices, axis=0, convert=True, is_copy=True, **kwargs):
"""
Analogous to ndarray.take
Return an object formed from the elements in the given indices along an
Copy link
Contributor

Choose a reason for hiding this comment

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

add that this is a positional selection.

Copy link
Member

Choose a reason for hiding this comment

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

Done.

name class max_speed
3 monkey mammal NaN
2 lion mammal 80.5

Returns
-------
Copy link
Contributor

Choose a reason for hiding this comment

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

add a See Also, showing ndarray.take

Copy link
Member

Choose a reason for hiding this comment

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

Done.

columns=('name', 'class', 'max_speed'))
>>> df
name class max_speed
0 falcon bird 389.0
Copy link
Contributor

Choose a reason for hiding this comment

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

add a comment that these are positional selections (where you think it is needed)

Copy link
Member

Choose a reason for hiding this comment

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

Done.

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame([('falcon', 'bird', 389.0),
('parrot', 'bird', 24.0),
Copy link
Contributor

Choose a reason for hiding this comment

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

give this an index (like [0, 3, 2, 1]) or something to emphasize that these are positional operations

Copy link
Member

Choose a reason for hiding this comment

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

Done.

@codecov
Copy link

codecov bot commented Jul 15, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@91245a7). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##             master   #16948   +/-   ##
=========================================
  Coverage          ?   91.01%           
=========================================
  Files             ?      162           
  Lines             ?    49567           
  Branches          ?        0           
=========================================
  Hits              ?    45114           
  Misses            ?     4453           
  Partials          ?        0
Flag Coverage Δ
#multiple 88.79% <ø> (?)
#single 40.24% <ø> (?)
Impacted Files Coverage Δ
pandas/core/generic.py 92.03% <ø> (ø)

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 91245a7...0a07775. Read the comment docs.

@jreback
Copy link
Contributor

jreback commented Jul 19, 2017

can you update

@gfyoung gfyoung added this to the 0.21.0 milestone Jul 19, 2017
@jorisvandenbossche
Copy link
Member

@matagus Do you have time to update this according to the feedback?
(if not, no problem, but please indicate this so somebody else can take over)

@jreback
Copy link
Contributor

jreback commented Aug 18, 2017

@gfyoung if you have a chance can you fix up.

@gfyoung gfyoung self-assigned this Aug 18, 2017
@gfyoung gfyoung force-pushed the docs-improving-dataframe-take-docstrings branch from 595cee8 to 9fb4aa4 Compare August 21, 2017 03:35
@gfyoung
Copy link
Member

gfyoung commented Aug 21, 2017

@jreback : Changes requested have been addressed. PTAL.

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.

Looks good! Some nitpicks

2 mammal 80.5
1 mammal NaN

We may take elements using negative integers for positive indices.
Copy link
Member

Choose a reason for hiding this comment

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

can you clarify that this is for starting at the end?

Copy link
Member

Choose a reason for hiding this comment

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

Done.

>>> df.take([-1, -2])
name class max_speed
1 monkey mammal NaN
2 lion mammal 80.5
Copy link
Member

Choose a reason for hiding this comment

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

it is a bit unfortunate in the example that it is exactly indices [1, 2] for [-1, -2], so maybe change the indices a bit so they don't match? (also because you said "We may take elements using negative integers for positive indices.", which can be interpreted that negative values are interpreted as the positive index of that value)

Copy link
Member

Choose a reason for hiding this comment

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

Done.


See Also
--------
ndarray.take
Copy link
Member

Choose a reason for hiding this comment

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

Does this need numpy. before it? (not sure, but for clarity maybe does no harm in any case)

Copy link
Member

Choose a reason for hiding this comment

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

Done.


This means that we are not indexing according to actual values in
the index attribute of the object. We are indexing according to the
actual position of the element in the object's array of values.
Copy link
Member

Choose a reason for hiding this comment

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

I would just write "actual position of the elements in the object"
("object's array of values" can possibly cause some confusion I think)

Copy link
Member

Choose a reason for hiding this comment

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

Done.

@gfyoung gfyoung force-pushed the docs-improving-dataframe-take-docstrings branch 2 times, most recently from e8f7bc3 to 0a07775 Compare August 21, 2017 08:25
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.

Perfect!

@jorisvandenbossche jorisvandenbossche merged commit d0d28fe into pandas-dev:master Aug 21, 2017
@jorisvandenbossche
Copy link
Member

Thanks @matagus and @gfyoung !

(btw, @gfyoung I personally find it easier that you add commits instead of amending the previous one, it's sometimes easier to see what has changes (in this case only small diff, so not really a problem, but for larger diffs can be helpful)

@gfyoung
Copy link
Member

gfyoung commented Aug 21, 2017

@jorisvandenbossche : Absolutely. Good to know.

On a separate note, what is the purpose of the convert parameter (I generally don't use it)? I see no difference in behavior AFAICT (using the docstring example):

>>> df.take([-1, -2])
             name   class  max_speed
        1  monkey  mammal        NaN
        3    lion  mammal       80.5
>>> df.take([-1, -2], convert=False)
             name   class  max_speed
        1  monkey  mammal        NaN
        3    lion  mammal       80.5

@gfyoung gfyoung removed their assignment Aug 21, 2017
@jorisvandenbossche
Copy link
Member

Good catch, reason is very simple, it is not passed through:

pandas/pandas/core/generic.py

Lines 2140 to 2142 in d0d28fe

new_data = self._data.take(indices,
axis=self._get_block_manager_axis(axis),
convert=True, verify=True)

But not sure if this should actually be a public parameter

@gfyoung
Copy link
Member

gfyoung commented Aug 21, 2017

Doh! Yeah, that's a good point. Drilling further down reveals more strangeness:

if verify:
if ((indexer == -1) | (indexer >= n)).any():
raise Exception('Indices must be nonzero and less than '
'the axis length')

The error message doesn't seem to match the check here IINM.

gfyoung added a commit to forking-repos/pandas that referenced this pull request Aug 27, 2017
xref pandas-devgh-16948. The parameter is not respected.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Aug 27, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Aug 27, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Aug 28, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Aug 28, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 23, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 24, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 25, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 25, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 25, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 26, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 27, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 27, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 28, 2017
[ci skip]

xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 29, 2017
[ci skip]

xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 29, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 29, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 29, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 29, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 30, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 30, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 30, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 30, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 30, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 30, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 30, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 30, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Sep 30, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Oct 1, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Oct 1, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
gfyoung added a commit to forking-repos/pandas that referenced this pull request Oct 1, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
jreback pushed a commit that referenced this pull request Oct 1, 2017
xref gh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
alanbato pushed a commit to alanbato/pandas that referenced this pull request Nov 10, 2017
alanbato pushed a commit to alanbato/pandas that referenced this pull request Nov 10, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
No-Stream pushed a commit to No-Stream/pandas that referenced this pull request Nov 28, 2017
xref pandas-devgh-16948. The parameter is not respected,
nor is it a parameter in many 'take' implementations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants