Skip to content

DOC: Summing by level on a category index produce extra columns. #36740

Closed
@gepcel

Description

@gepcel

Generate a dataframe:

import pandas as pd
import numpy as np
column_cat = pd.CategoricalDtype(['One', 'Two', 'Three', 'Four'], ordered=True)
groups = list('AABBB')
numbers = pd.Categorical(['One', 'Two','One', 'Two', 'Three'], 
                         categories=column_cat.categories, ordered=True)
d = pd.DataFrame(data=np.arange(15).reshape(3, 5), 
                 columns=pd.MultiIndex.from_arrays([groups, numbers]))
A B
One Two One Two Three
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14

Now do d.sum(axis=1, level=1), get the following result:

One Two Three Four
0 2 4 4 0
1 12 14 9 0
2 22 24 14 0

Which produces a non existing column Four.

pandas 1.1.1, pandas 1.1.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions