Skip to content

ENH: Allow multi values for index and columns in df.pivot #30928

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 24 commits into from
Feb 9, 2020

Conversation

charlesdong1991
Copy link
Member

@charlesdong1991 charlesdong1991 commented Jan 11, 2020

@pep8speaks
Copy link

pep8speaks commented Jan 11, 2020

Hello @charlesdong1991! 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 2020-02-02 19:24:06 UTC

@gfyoung gfyoung added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jan 15, 2020
@charlesdong1991
Copy link
Member Author

would be nice if someone could take a look here, i just rebased ^^

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

can you add an example in the docs as well

@@ -228,6 +228,7 @@ Other enhancements
- Added an experimental :attr:`~DataFrame.attrs` for storing global metadata about a dataset (:issue:`29062`)
- :meth:`Timestamp.fromisocalendar` is now compatible with python 3.8 and above (:issue:`28115`)
- :meth:`DataFrame.to_pickle` and :func:`read_pickle` now accept URL (:issue:`30163`)
- :meth:`DataFrame.pivot` can now take lists for ``index`` and ``columns`` arguments (:issue:`21425`)
Copy link
Contributor

Choose a reason for hiding this comment

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

move to 1.1

@@ -5847,11 +5847,19 @@ def groupby(

Parameters
----------%s
index : str or object, optional
index : str or object or a list of the previous, optional
Copy link
Contributor

Choose a reason for hiding this comment

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

list of str, this really takes object? what exactly does that mean

Copy link
Member Author

Choose a reason for hiding this comment

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

oh, my bad, i copied the description from values, will update a bit to make it clearer

@charlesdong1991
Copy link
Member Author

just in case if you'd like to take a look too @WillAyd thanks! ^^

Column to use to make new frame's index. If None, uses
existing index.
columns : str or object

.. versionchanged:: 1.0.0
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
.. versionchanged:: 1.0.0
.. versionchanged:: 1.1.0

Copy link
Member Author

@charlesdong1991 charlesdong1991 Jan 21, 2020

Choose a reason for hiding this comment

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

nice catch!! my bad 😅

Column to use to make new frame's columns.

.. versionchanged:: 1.0.0
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
.. versionchanged:: 1.0.0
.. versionchanged:: 1.1.0

Copy link
Member Author

Choose a reason for hiding this comment

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

done!


if values is None:
cols = [columns] if index is None else [index, columns]
cols: List[str] = []
Copy link
Member

Choose a reason for hiding this comment

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

I think you want List[Label] (import from pandas._typing) to allow anything column name, unless this really does have to be string-only

Copy link
Contributor

Choose a reason for hiding this comment

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

i don't think this will matter until we type the signature, so ok with handling this as a followon.

@@ -1007,6 +1007,192 @@ def test_pivot_no_level_overlap(self):
expected = grouped.unstack("b").unstack("c").dropna(axis=1, how="all")
tm.assert_frame_equal(table, expected)

@pytest.mark.parametrize(
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 split this out (and other multiindex uses of pivot) to test_pivot_multilevel.py

Copy link
Contributor

Choose a reason for hiding this comment

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

we can do this in a followon.

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks! I think a bit, and feel maybe it's better to make this change here to make the scope of PR clearer, so i made a new file test_pivot_multilevel.py and moved related tests there.

@@ -431,17 +431,31 @@ def _convert_by(by):
def pivot(data: "DataFrame", index=None, columns=None, values=None) -> "DataFrame":
if columns is None:
raise TypeError("pivot() missing 1 required argument: 'columns'")
columns = columns if is_list_like(columns) else [columns]
Copy link
Contributor

Choose a reason for hiding this comment

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

asa followon can you try to type tings in the signature


if values is None:
cols = [columns] if index is None else [index, columns]
cols: List[str] = []
Copy link
Contributor

Choose a reason for hiding this comment

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

i don't think this will matter until we type the signature, so ok with handling this as a followon.

@charlesdong1991
Copy link
Member Author

HI, @jreback and @WillAyd thanks for both of yours reviews, appreciate a lot!!

One thing to mention: i'd like to have signature added as a follow-up to make this clearer, since we have PRs addressing TYPING issues only if you agree. Otherwise, I think I have addressed both of your reviews in the change ^^

Feel free for further reviews! Thanks!

@charlesdong1991
Copy link
Member Author

just updated with latest master

@charlesdong1991
Copy link
Member Author

gentle ping @WillAyd for feedback? ^^

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

lgtm @jreback

@charlesdong1991
Copy link
Member Author

gentle ping @jreback ^^

@charlesdong1991
Copy link
Member Author

sorry for pushing for a review, I know you are so busy with so many PRs. This PR has been rebased several times, so some feedbacks would be appreciated @jreback 👍

@jreback jreback added this to the 1.1 milestone Feb 9, 2020
@jreback jreback merged commit 11f1500 into pandas-dev:master Feb 9, 2020
@jreback
Copy link
Contributor

jreback commented Feb 9, 2020

thanks @charlesdong1991
very nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: DataFrame.pivot fails on multiple columns to set as index
5 participants