Skip to content

Add examples to documentation for DataFrame.swaplevel #40269

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

Conversation

thordisstella
Copy link
Contributor

@pep8speaks
Copy link

pep8speaks commented Mar 6, 2021

Hello @thordisstella! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-03-11 20:58:01 UTC

@thordisstella thordisstella force-pushed the add_examples_for_swaplevel branch 2 times, most recently from 157db00 to fbb1755 Compare March 6, 2021 12:46
@MarcoGorelli MarcoGorelli self-requested a review March 6, 2021 16:15
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Really nice example @thordisstella , thanks, have just left some minor comments

in a similar manner. Note that column-wise is the default behaviour.
By not supplying any arguments for i and j, we swap the last and second to
last indices.
>>> df.swaplevel()
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 leave a blank line before this?

Comment on lines 6284 to 6288
>>> df = pd.DataFrame({'Grade':['A','B','A','C']},
... index=[['Final exam', 'Final exam', 'Coursework',
... 'Coursework'],
... ['History','Geography','History','Geography'],
... ['January','February','March','April']])
Copy link
Member

Choose a reason for hiding this comment

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

Could you reformat this according to the black formatter (to be consistent with most of the rest of the docs...we should probably run blackdoc, but that's for a separate issue/PR)

We can also define explicitly which indices we want to swap by supplying values
for both i and j. Here, we for example swap the first and second indices.

>>> df.swaplevel(0,1)
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 add a space between the arguments? (swaplevel(0, 1))

@thordisstella thordisstella force-pushed the add_examples_for_swaplevel branch from fbb1755 to 3577010 Compare March 8, 2021 17:58
@thordisstella
Copy link
Contributor Author

Really nice example @thordisstella , thanks, have just left some minor comments

Thanks for the review @MarcoGorelli! I addressed the comments, but when I ran black on the changes I'd made, nothing further was updated (after I'd addressed your other two comments). Please let me know if I've missed something here

@MarcoGorelli MarcoGorelli self-requested a review March 8, 2021 20:47
@MarcoGorelli
Copy link
Member

MarcoGorelli commented Mar 8, 2021

Hey @thordisstella , thanks for updating (and happy IWD!)! Sorry, I was unclear - black indeed won't modify what you've written, because it doesn't format code inside docstrings (barring some very minor things). I was suggesting to reformat the docstring the way that black would - one way to do this would be to paste the docstring into a temporary file (say, t.py):

# t.py

"""
        >>> df = pd.DataFrame({'Grade':['A','B','A','C']},
        ...                   index=[['Final exam', 'Final exam', 'Coursework',
        ...                              'Coursework'],
        ...                         ['History','Geography','History','Geography'],
        ...                         ['January','February','March','April']])
"""

, running

pip install blackdoc
blackdoc t.py --line-length 80

and then pasting the reformatted version back into this docstring of swaplevel

Finally, you should run

$ python scripts/validate_docstrings.py pandas.DataFrame.swaplevel

and check it doesn't throw any extra errors compared to when run on master (in this case you prob. just need to reformat the code in the example and remove the extra blankline at the end of the docstring)

@thordisstella thordisstella force-pushed the add_examples_for_swaplevel branch from 3577010 to 55832da Compare March 9, 2021 09:46
@thordisstella
Copy link
Contributor Author

Thanks, and belated happy IWD to you too! 😊

Thanks for the clarification - I've updated the PR as you suggested so hopefully we should be good now 🤞

Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Thanks - there's just a blank line which needs removing (else it'll fail scripts/validate_docstrings.py), rest looks good

Comment on lines 6331 to 6332
Geography Coursework April C

"""
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Geography Coursework April C
"""
Geography Coursework April C
"""

@thordisstella thordisstella force-pushed the add_examples_for_swaplevel branch from 55832da to 302a2f0 Compare March 9, 2021 11:15
@MarcoGorelli
Copy link
Member

Hi @thordisstella - looks good, CI failures should be unrelated, could you fetch and merge upstream/master please?

@MarcoGorelli MarcoGorelli self-requested a review March 11, 2021 20:51
Copy link
Member

@MarcoGorelli MarcoGorelli left a comment

Choose a reason for hiding this comment

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

Pending green, looks good to me!

(a follow-up PR to add a similar example to Series.swaplevel and/or make a shared docstring would be welcome if you want)

@MarcoGorelli MarcoGorelli added this to the 1.3 milestone Mar 11, 2021
@MarcoGorelli MarcoGorelli merged commit 0a2a200 into pandas-dev:master Mar 12, 2021
@thordisstella
Copy link
Contributor Author

Thanks for the review, all the advice and for merging this @MarcoGorelli ! And apologies for the delay in reacting to your last comment. I'll take a look at the follow up PR you suggested either later this week or next week :)

@thordisstella thordisstella deleted the add_examples_for_swaplevel branch March 15, 2021 13:58
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.

3 participants