Groupby on multiindex with missing data in group keys raises IndexError · Issue #20519 · pandas-dev/pandas (original) (raw)

Code Sample, a copy-pastable example if possible

foo= pd.DataFrame([['x', np.nan, 1]], columns = ['A', 'B', 'C']).set_index(['A', 'B']) foo.groupby(level = ['A', 'B']).C.sum() IndexError: cannot do a non-empty take from an empty axes.

while this is expected, as a comparison

bar = pd.DataFrame([['x', np.nan, 1]], columns = ['A', 'B', 'C']) bar.groupby(level = ['A', 'B']).C.sum() Series([], Name: C, dtype: int64)

Problem description

It would be better if pandas can either return the same result as the 'bar' case, or raise another meaningful exception and document this case somewhere (e.g. here))

Expected Output

Series([], Name: C, dtype: int64)

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.12.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-229.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.1
nose: None
pip: None
setuptools: None
Cython: None
numpy: 1.10.4
scipy: None
statsmodels: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2016.4
blosc: None
bottleneck: 1.0.0
tables: None
numexpr: 2.4.6
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: 0.9.2
apiclient: 1.2
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
boto: 2.22.1
pandas_datareader: None