Code Sample, a copy-pastable example if possible

import pandas as pd
mi = pd.MultiIndex.from_tuples([['b','d'],['b','c'],['b','c'],['a','c']], names=list('AB'))
mi = mi.set_levels(pd.CategoricalIndex(mi.levels[0], categories=['b','a']), level='A')
df = pd.DataFrame([[1,1,1,1],[1,1,1,1]], columns=mi)
df.sum(axis=1, level=[0,1])

returns:

A  b     a    
B  c  d  c   d
0  2  1  1 NaN
1  2  1  1 NaN

Problem description

A spurious column (a,d) containing all NaNs is included in the result. Also, the ordering of the second column level is no longer correct.

Expected Output

The same result as when level 'A' would not have been a CategoricalIndex:

import pandas as pd
mi = pd.MultiIndex.from_tuples([['b','d'],['b','c'],['b','c'],['a','c']], names=list('AB'))
df = pd.DataFrame([[1,1,1,1],[1,1,1,1]], columns=mi)
df.sum(axis=1, level=[0,1])
````
which correctly returns:

A b a B d c c 0 1 2 1 1 1 2 1 ```

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.6.6.final.0 python-bits : 64 OS : Windows OS-release : 10 machine : AMD64 processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : None.None pandas : 0.25.1 numpy : 1.17.1 pytz : 2019.2 dateutil : 2.8.0 pip : 19.2.3 setuptools : 39.0.1 Cython : 0.29.4 pytest : 4.2.1 hypothesis : None sphinx : None blosc : None feather : None xlsxwriter : 1.1.2 lxml.etree : 4.3.4 html5lib : 1.0.1 pymysql : None psycopg2 : None jinja2 : 2.10.1 IPython : 5.8.0 pandas_datareader: None bs4 : 4.6.1 bottleneck : 1.2.1 fastparquet : None gcsfs : None lxml.etree : 4.3.4 matplotlib : 2.2.4 numexpr : 2.6.9 odfpy : None openpyxl : 2.5.12 pandas_gbq : None pyarrow : None pytables : None s3fs : None scipy : 1.3.1 sqlalchemy : 1.2.18 tables : None xarray : 0.12.3 xlrd : 1.2.0 xlwt : 1.3.0 xlsxwriter : 1.1.2

Comment From: kdebrab

This seems to have been resolved in the meantime. Both in Python 3.6 (pandas version 0.25.1) as in Python 2.7 (pandas version 0.24.2), I now get the expected result when executing the code sample. The spurious column doesn't appear anymore.

Comment From: kdebrab

Maybe not, it rather seems like the example doesn't really work anymore. It seems like mi = mi.set_levels(pd.Categorical(mi.levels[0], categories=['b','a']), level='A') does no longer have the intended effect...

Comment From: kdebrab

OK, I see: I have to replace pd.Categorical() by pd.CategoricalIndex() in order to reproduce the issue. Conclusion: issue has not been solved yet! I updated the code sample accordingly.

Comment From: jbrockmendel

The level keyword is no longer present in DataFrame reductions, and df.groupby(level=[0,1], axis=1).sum() looks right to me. Closing.