Skip to content

BUG: MutliIndex names lost during pivot #36360

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

Closed
2 of 3 tasks
r7sa opened this issue Sep 14, 2020 · 0 comments · Fixed by #36655
Closed
2 of 3 tasks

BUG: MutliIndex names lost during pivot #36360

r7sa opened this issue Sep 14, 2020 · 0 comments · Fixed by #36655
Labels
Bug MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@r7sa
Copy link

r7sa commented Sep 14, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

d = pd.DataFrame([[1,2,3],[2,2,3],[3,2,3],[4,2,3],[5,2,3]], 
                 columns=pd.MultiIndex.from_tuples([[0, 0, 0], [0, 0, 1], [0, 0, 2]], names=['c1', 'c2', 'c3']),
                 index=pd.MultiIndex.from_tuples([[0, 0, 0, 0, 0, 0, 0],
                                                  [0, 0, 1, 0, 0, 0, 1],
                                                  [0, 1, 0, 0, 0, 1, 0],
                                                  [0, 1, 1, 0, 0, 1, 1],
                                                  [1, 0, 0, 0, 1, 0, 0]
                                                  ], 
                                                 names=['i1', 'i2', 'i3', 'i4', 'i5', 'i6', 'i7']))
e = d.pivot_table(index=['i1',], columns=['i2', 'i3',], values=[(0, 0, 1), (0, 0, 2)], aggfunc=lambda v: v.values[0])
e.columns

result is:

            (0, 0, 1, 0, 1),
            (0, 0, 1, 1, 0),
            (0, 0, 1, 1, 1),
            (0, 0, 2, 0, 0),
            (0, 0, 2, 0, 1),
            (0, 0, 2, 1, 0),
            (0, 0, 2, 1, 1)],
           names=[None, None, None, 'i2', 'i3'])

some names is None, but must be
names=['c1', 'c2', 'c3', 'i2', 'i3'])

Problem description

Problems seems in pandas/core/groupby/generic.py in class DataFrameGroupBy in function _wrap_aggregated_output
Line

name = self._obj_with_exclusions._get_axis(1 - self.axis).name

seems to must be

name_axis = self._obj_with_exclusions._get_axis(1 - self.axis)
name = name_axis.names if isinstance(name_axis, MultiIndex) else name_axis.name
@r7sa r7sa added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 14, 2020
@dsaxton dsaxton changed the title BUG: BUG: MutliIndex names lost during pivot Sep 14, 2020
@dsaxton dsaxton added MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 14, 2020
@jreback jreback added this to the 1.2 milestone Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
3 participants