Skip to content

Dataframe pivot_table() returning a multi-index for a single value, empty dataframe #13483

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
amanhanda opened this issue Jun 18, 2016 · 2 comments · Fixed by #39940
Closed
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@amanhanda
Copy link

Code Sample, a copy-pastable example if possible

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'a': ['a1', 'a2', 'a3'], 'b': [1,2,3], 'c':['c1', 'c2', 'c3']})

In [3]: x = df[df['c'] == 'c1']

In [4]: y = x.pivot_table(index='a', columns='c', values='b')

In [5]: y.columns
Out[5]: Index([u'c1'], dtype='object', name=u'c')
In [6]: x = df[df['c'] == 'c4']

In [7]: y = x.pivot_table(index='a', columns='c', values='b')

In [8]: y.columns  # returns a multi-index, should not
Out[8]:
MultiIndex(levels=[[u'b'], []],
           labels=[[], []],
           names=[None, u'c'])

# a check was added for empty dataframe that is causing the behavior, works in 0.16.2
> /auto/energymdl2/anaconda/envs/commod_20160516_pd18/lib/python2.7/site-packages/pandas/tools/pivot.py(161)pivot_table()
    159
    160     # discard the top level
--> 161     if values_passed and not values_multi and not table.empty:
    162         table = table[values[0]]
    163


Expected Output

In [5]: y.columns
Out[5]: Index([], dtype='object', name=u'c')

output of pd.show_versions()


INSTALLED VERSIONS
------------------
commit: None
python: 2.7.11.final.0
python-bits: 64
OS: Linux
OS-release: 2.6.32-573.7.1.el6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: C
LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.1
setuptools: 20.7.0
Cython: 0.24
numpy: 1.10.4
scipy: 0.17.0
statsmodels: 0.6.1
xarray: 0.7.2
IPython: 4.1.2
sphinx: 1.3.5
patsy: 0.4.1
dateutil: 2.5.2
pytz: 2016.4
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.5.2
matplotlib: 1.4.3
openpyxl: 2.3.2
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.4
lxml: 3.6.0
bs4: 4.3.2
html5lib: 0.999
httplib2: 0.9.2
apiclient: 1.5.0
sqlalchemy: 1.0.12
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.39.0
pandas_datareader: None

@amanhanda
Copy link
Author

Is there a reason for having the additional check for empty table? Would the fix be to remove the additional condition?

    159
    160     # discard the top level
--> 161     if values_passed and not values_multi and not table.empty:
    162         table = table[values[0]]
    163

@sinhrks sinhrks added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jul 4, 2016
@sinhrks
Copy link
Member

sinhrks commented Jul 4, 2016

This was added in #12287 and simply removing it causes another problem. May better to reconsider whole block (related to #13554).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants