-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: DataFrame.pivot drops column level names when both rows and columns are multiindexed #36655
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
6004334
to
fa879b7
Compare
pandas/core/groupby/generic.py
Outdated
|
||
agg_axis = self._obj_with_exclusions._get_axis(1 - self.axis) | ||
if isinstance(agg_axis, MultiIndex): | ||
columns = Index([key.label for key in output], names=agg_axis.names) |
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.
does this line alone work? I thought we fixed construction for this. alt does ensure_index
work here? I really do not like special casing like this anywhere except in the index module itself (and even then its not great).
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.
It does! I've removed the if-else clause
|
||
|
||
def test_pivot_multiindexed_rows_and_cols(): | ||
# GH 36360 |
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.
do we have appropraite tests if index OR columns are a MI and the other is not?
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 do (in this file and in test_pivot.py
)
What existing tests don't cover is the case when pivot_table
produces a result with multiple MutliIndexed columns - added here
390e9a9
to
8dd1161
Compare
pandas/core/indexes/base.py
Outdated
@@ -404,6 +404,11 @@ def __new__( | |||
) | |||
# other iterable of some kind | |||
subarr = com.asarray_tuplesafe(data, dtype=object) | |||
|
|||
if isinstance(name, list) and len(name) == 1: |
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 do not want to add this here, this is a very complicated routine; what are you trying to accomplish?
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 was looking for the functionality of Index._get_names()
/ MultiIndex._get_names()
Reverted this + using _get_names
now
861baa9
to
ffb4540
Compare
@jreback this one is ready for re-review. I now handle simple index and multiindex together with |
thanks @arw2019 |
…mns are multiindexed (pandas-dev#36655)
…mns are multiindexed (pandas-dev#36655)
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
Fixed by checking for multi-indexed columns in
DataFrameGroupBy._wrap_aggregated_output