Code Sample, a copy-pastable example if possible
# Part 1
df1 = pd.DataFrame([], columns=['a', 'b', 'value'])
pivot1 = df1.pivot_table(index='a', columns='b', values='value',
aggfunc='count')
print(pivot1.columns)
#Output:
MultiIndex(levels=[['value'], []],
labels=[[], []],
names=[None, 'b'])
# Part 2
df2 = pd.DataFrame([[1, 2, 3]], columns=['a', 'b', 'value'])
pivot2 = df2.pivot_table(index='a', columns='b', values='value',
aggfunc='count')
print(pivot2.columns)
#Output:
Int64Index([2], dtype='int64', name='b')
Problem description
In the first example, I don't expect to see any multiindex in .columns
, as exactly one value for columns is provided. As we don't have any data, and it is not possible to figure out the dtype
of this index, one can probably assume it's something like Index([], name='b')
.
Output of pd.show_versions()
Comment From: gfyoung
Indeed, that does look a little odd! Perhaps a corner case?
cc @jreback
Comment From: mroeschke
Looks to work on master now. Could use a test
In [10]: df1 = pd.DataFrame([], columns=['a', 'b', 'value'])
...: pivot1 = df1.pivot_table(index='a', columns='b', values='value',
...: aggfunc='count')
...: print(pivot1.columns)
Index([], dtype='object', name='b')
Comment From: FaazAbidi
Is anyone working on it? I can take this. @mroeschke
Comment From: mroeschke
Go for it @FaazAbidi
Comment From: FaazAbidi
take
Comment From: FaazAbidi
@mroeschke I think this test belongs to tests.frame.test_repr_info.py. Can you confirm?
Comment From: mroeschke
Probably pandas/tests/reshape/test_pivot.py
Comment From: MirijaH
take