Skip to content

Conversation

@bkjchoi72
Copy link
Contributor

@bkjchoi72 bkjchoi72 commented Oct 3, 2018

Fixed the docstring of the function _set_axis_name in pandas/core/generic.py

Closes #22895

@pep8speaks
Copy link

pep8speaks commented Oct 3, 2018

Hello @bkjchoi72! Thanks for updating the PR.

Comment last updated on October 09, 2018 at 08:34 Hours UTC
@codecov
Copy link

codecov bot commented Oct 5, 2018

Codecov Report

Merging #22969 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@ Coverage Diff @@ ## master #22969 +/- ## ========================================== + Coverage 92.28% 92.28% +<.01%  ========================================== Files 161 161 Lines 51451 51452 +1 ========================================== + Hits 47483 47484 +1  Misses 3968 3968
Flag Coverage Δ
#multiple 90.68% <100%> (ø) ⬆️
#single 42.28% <100%> (ø) ⬆️
Impacted Files Coverage Δ
pandas/core/generic.py 96.84% <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 deb7b4d...9d944e8. Read the comment docs.

@gfyoung gfyoung added the Docs label Oct 6, 2018
Copy link
Member

@gfyoung gfyoung left a comment

Choose a reason for hiding this comment

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

Nice!

cc @datapythonista

@gfyoung
Copy link
Member

gfyoung commented Oct 6, 2018

@bkjchoi72 : FYI, you don't have to close your PR (#22968) and open a new if there are issues with it. You can keep making commits to the existing PR and fix them there.

The axis to set the label. The value 0 or 'index' specifies index,
and the value 1 or 'columns' specifies columns.
inplace : bool, default False
Whether to modify `self` directly or return a copy.
Copy link
Contributor

Choose a reason for hiding this comment

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

see what we use elsewhere for this

Copy link
Contributor Author

@bkjchoi72 bkjchoi72 Oct 9, 2018

Choose a reason for hiding this comment

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

There isn't great consistency for the description of inplace parameter. The most common and easy to understand I found is If True, do operation inplace and return None. I'll go with that one, and I'll change bool to boolean.

Examples
--------
>>> df = pd.DataFrame({"A": [1, 2, 3]})
>>> df._set_axis_name("foo")
Copy link
Contributor

Choose a reason for hiding this comment

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

can you show what the current axis name is prior to setting it.

Examples
--------
>>> df = pd.DataFrame({"A": [1, 2, 3]})
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for updating the docstring @bkjchoi72, looking much better now, and it fixes the doctest, which was the main goal. Just one comment that would make this docstring even better, besides the comments in the previous review.

Do you mind adding a more real-world example? I know it's the original example, not yours, but setting the axis name to foo doesn't give a good idea of that the axis name is.

We use examples in the code with names of animals, and some property like the number of legs. Could you do something like that, and set the axis to animals or the meaningful name for the example?

If you can also change that in DataFrame.rename_axis() too, which has a example with foo as well, that would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I'll make those changes.

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

looking good, just two small things

0 dog 4
1 cat 4
2 monkey 2
>>> df.rename_axis("id", axis="columns")
Copy link
Member

Choose a reason for hiding this comment

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

In this case, the concept id refers to name and legs, which does not make sense.

It could make more sense to have the name as the index, and then may be a column num_legs and a column num_arms and call the axis limbs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense

0 1 4
1 2 5
2 3 6
>>> df = pd.DataFrame({"name": ["dog", "cat", "monkey"], "legs": [4, 4, 2]})
Copy link
Member

Choose a reason for hiding this comment

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

this is longer than 79 characters, isn't it?

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

The example looks great. Added some more comments, minor things that can be improved, and then should be ready to be merged.

1 2 5
2 3 6
>>> df = pd.DataFrame({"num_legs": [4, 4, 2],
... "num_arms": [0, 0, 2]},
Copy link
Member

Choose a reason for hiding this comment

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

the indentation of this line looks wrong

def _set_axis_name(self, name, axis=0, inplace=False):
"""
Alter the name or names of the axis.
Alter the label(s) of the axis.
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem correct. Altering the labels would be changing the names of the columns, not the name of the axis itself.

And small thing, but alter doesn't necessarily need to be correct. If the axis doesn't have a name, it's setting it, but not altering. Set would be better I think.

0 or 'index' for the index; 1 or 'columns' for the columns
inplace : bool
whether to modify `self` directly or return a copy
Labels(s) to set.
Copy link
Member

Choose a reason for hiding this comment

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

Same as before, label is the names of each individual column, not the name of the axis

Returns
-------
renamed : same type as caller or None if inplace=True
Series, DataFrame, Panel, or None
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 remove Panel here. While it's correct, Panel it's already deprecated and will disappear in couple of months, so not worth having it.

cat 4
monkey 2
>>> df.index = pd.MultiIndex.from_product(
... [["mammal"], ['dog', 'cat', 'monkey']])
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 try to present this using a better indentation. I think this is not pep8 compatible

@bkjchoi72
Copy link
Contributor Author

@jreback @datapythonista Thanks for the patience guys. Let me make adjustments based on your feedback.

Copy link
Member

@datapythonista datapythonista left a comment

Choose a reason for hiding this comment

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

lgtm, thanks @bkjchoi72 for the fixes

@datapythonista
Copy link
Member

@bkjchoi72 can you take a look at the errors in travis please? they look like genuine errors in your PR.

@bkjchoi72
Copy link
Contributor Author

@datapythonista The tests are fixed. Thanks for your time and guidance!

@datapythonista
Copy link
Member

did you run flake8 --doctests pandas/core/generic.py? I think there is a wrong indentation in your example (note that the command will show errors in other parts of the file that you can ignore).

@bkjchoi72
Copy link
Contributor Author

@datapythonista I don't see indentation errors related to my changes. I modified lines 1120 - 1191 and 1207 -1258. Below is the output of flake8 --doctests pandas/core/generic.py.

$ flake8 --doctests pandas/core/generic.py pandas/core/generic.py:659:13: F821 undefined name 'p' pandas/core/generic.py:660:13: F821 undefined name 'p' pandas/core/generic.py:3195:13: F821 undefined name 'df' pandas/core/generic.py:3200:13: F821 undefined name 'df' pandas/core/generic.py:3205:13: F821 undefined name 'df' pandas/core/generic.py:3211:13: F821 undefined name 'df' pandas/core/generic.py:3218:13: F821 undefined name 'df' pandas/core/generic.py:3222:13: F821 undefined name 'df' pandas/core/generic.py:3227:13: F821 undefined name 'df' pandas/core/generic.py:7545:71: F721 syntax error in doctest pandas/core/generic.py:7548:13: F821 undefined name 's' pandas/core/generic.py:7556:13: F821 undefined name 's' pandas/core/generic.py:7567:13: F821 undefined name 's' pandas/core/generic.py:7599:52: F721 syntax error in doctest pandas/core/generic.py:7603:13: F821 undefined name 'df2' 

None in the range that I modified shows up from my end. Do you have specific line numbers that are violating pep8 standards?

2 3 6
>>> df = pd.DataFrame({"num_legs": [4, 4, 2],
... "num_arms": [0, 0, 2]},
... ["dog", "cat", "monkey"])
Copy link
Member

Choose a reason for hiding this comment

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

this is where I think the ['dog'... should be indented as the {, and not one more. Not sure why is not being captured.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like 1173 and 1174 both need to be aligned with {

Copy link
Member

Choose a reason for hiding this comment

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

doesn't make sense to me, I think it's a flake8 bug, but let's leave it like this is the linting fails with what I think it's the right indentaiton.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, it makes sense to adjust line 1174 only since the argument is composed of two things: dict and list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

flake8 still passes with the suggested indentation.

@datapythonista
Copy link
Member

Sorry for the delay @bkjchoi72, can you fix the conflicts, so we can merge?

@datapythonista
Copy link
Member

@bkjchoi72, do you have time to fix the conflicts?

@bkjchoi72
Copy link
Contributor Author

@datapythonista I'll get to it this weekend.

@datapythonista
Copy link
Member

Rebased, and added some more fixes to the docstring.

@Dr-Irv @jreback can you take a look?

@jreback jreback added this to the 0.24.0 milestone Nov 20, 2018
@jreback jreback merged commit 7a6ecf7 into pandas-dev:master Nov 20, 2018
@jreback
Copy link
Contributor

jreback commented Nov 20, 2018

thanks @bkjchoi72

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Nov 26, 2018

@datapythonista I was on vacation for the past 10 days, so just saw this now. All looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6 participants