8000 Pandas pivot_table MultiIndex and dropna=False generates all combinations of modalities instead of keeping existing one only by randolf-scholz · Pull Request #45994 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Pandas pivot_table MultiIndex and dropna=False generates all combinations of modalities instead of keeping existing one only #45994

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
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix #18030
  • Loading branch information
Randolf Scholz committed Feb 15, 2022
commit b63e452a54827670a4f4e3fdfa32194c0d977d8f
38 changes: 26 additions & 12 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,32 @@ def __internal_pivot_table(
to_unstack.append(name)
table = agged.unstack(to_unstack)

if not dropna:
if isinstance(table.index, MultiIndex):
m = MultiIndex.from_arrays(
cartesian_product(table.index.levels), names=table.index.names
)
table = table.reindex(m, axis=0)

if isinstance(table.columns, MultiIndex):
m = MultiIndex.from_arrays(
cartesian_product(table.columns.levels), names=table.columns.names
)
table = table.reindex(m, axis=1)
# BEGIN Issue #18030
# Commenting these lines in view of #18030.
# The cartesian product makes little sense and deprecation of this behaviour should be considered.
# It is unexpected that a pivot operation returns a larger index than is given.
# Secondly, this behaviour was never documented. The documentation says
#
# dropna:bool, default True
#
# Do not include columns whose entries are all NaN.
#
# However the code below creates rows.

# if not dropna:
# if isinstance(table.index, MultiIndex):
# m = MultiIndex.from_arrays(
# cartesian_product(table.index.levels), names=table.index.names
# )
# table = table.reindex(m, axis=0)
#
# if isinstance(table.columns, MultiIndex):
# m = MultiIndex.from_arrays(
# cartesian_product(table.columns.levels), names=table.columns.names
# )
# table = table.reindex(m, axis=1)

# END Issue #18030

if isinstance(table, ABCDataFrame):
table = table.sort_index(axis=1)
Expand Down
0