Skip to content
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

pivot_table produces inconsistent columns if applied to empty table #21932

Closed
Tracked by #7
ischurov opened this issue Jul 16, 2018 · 8 comments · Fixed by #52809
Closed
Tracked by #7

pivot_table produces inconsistent columns if applied to empty table #21932

ischurov opened this issue Jul 16, 2018 · 8 comments · Fixed by #52809
Assignees
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions

Comments

@ischurov
Copy link
Contributor

ischurov commented Jul 16, 2018

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()

INSTALLED VERSIONS ------------------ commit: None python: 3.7.0.final.0 python-bits: 64 OS: Darwin OS-release: 17.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.23.3
pytest: None
pip: 10.0.1
setuptools: 39.2.0
Cython: None
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@gfyoung gfyoung added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Jul 16, 2018
@gfyoung
Copy link
Member

gfyoung commented Jul 16, 2018

Indeed, that does look a little odd! Perhaps a corner case?

cc @jreback

@mroeschke
Copy link
Member

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')

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jun 20, 2021
@FaazAbidi
Copy link

FaazAbidi commented Jul 14, 2022

Is anyone working on it? I can take this. @mroeschke

@mroeschke
Copy link
Member

Go for it @FaazAbidi

@FaazAbidi
Copy link

take

@FaazAbidi
Copy link

@mroeschke I think this test belongs to tests.frame.test_repr_info.py. Can you confirm?

@mroeschke
Copy link
Member

Probably pandas/tests/reshape/test_pivot.py

@MirijaH
Copy link
Contributor

MirijaH commented Apr 20, 2023

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants