Dataframe pivot_table() returning a multi-index for a single value, empty dataframe · Issue #13483 · pandas-dev/pandas (original) (raw)

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

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


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