-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Conversation
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 |
would be nice if someone could take a look here, i just rebased ^^ |
There was a problem hiding this 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
doc/source/whatsnew/v1.0.0.rst
Outdated
@@ -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`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to 1.1
pandas/core/frame.py
Outdated
@@ -5847,11 +5847,19 @@ def groupby( | |||
|
|||
Parameters | |||
----------%s | |||
index : str or object, optional | |||
index : str or object or a list of the previous, optional |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
just in case if you'd like to take a look too @WillAyd thanks! ^^ |
pandas/core/frame.py
Outdated
Column to use to make new frame's index. If None, uses | ||
existing index. | ||
columns : str or object | ||
|
||
.. versionchanged:: 1.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.. versionchanged:: 1.0.0 | |
.. versionchanged:: 1.1.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch!! my bad 😅
pandas/core/frame.py
Outdated
Column to use to make new frame's columns. | ||
|
||
.. versionchanged:: 1.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.. versionchanged:: 1.0.0 | |
.. versionchanged:: 1.1.0 |
There was a problem hiding this comment.
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] = [] |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
pandas/tests/reshape/test_pivot.py
Outdated
@@ -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( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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] = [] |
There was a problem hiding this comment.
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.
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! |
just updated with latest master |
gentle ping @WillAyd for feedback? ^^ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm @jreback
gentle ping @jreback ^^ |
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 👍 |
thanks @charlesdong1991 |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff