question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pivot_table produces inconsistent columns if applied to empty table

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mroeschkecommented, Jul 16, 2022

Probably pandas/tests/reshape/test_pivot.py

1reaction
mroeschkecommented, Jul 14, 2022

Go for it @FaazAbidi

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Troubleshoot and Fix Excel Pivot Table Errors
In the Create PivotTable dialog box, check the Table/Range selection to make sure you haven't selected blank columns beside the data table.
Read more >
[Solved] Excel corrupts file after refreshing Pivot table - LinkedIn
[Solved] Excel corrupts file after refreshing Pivot table · Here are some reasons along with the solutions: · Reason #1 - Empty/Blank Cells....
Read more >
Pivot Table Tips | Exceljet
To minimize problems down the road, make sure your data is in good shape. Source data should have no blank rows or columns,...
Read more >
Dynamic Pivot Tables in SQL Server - SQLShack
For instance, if a new subject value is inserted into the table, the pivot table won't be able to display the new value...
Read more >
Combine Files With Inconsistent Columns In Power Query
Learn how to combine files in a folder when the data in the files has inconsistent column headings and table names.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found