From b63e452a54827670a4f4e3fdfa32194c0d977d8f Mon Sep 17 00:00:00 2001 From: Randolf Scholz Date: Tue, 15 Feb 2022 02:48:49 +0100 Subject: [PATCH 1/2] fix #18030 --- pandas/core/reshape/pivot.py | 38 ++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index b428155e722ff..22f8178570656 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -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) From b01646385a3a4850034dc7e3b25c156319a4955e Mon Sep 17 00:00:00 2001 From: Randolf Scholz Date: Tue, 15 Feb 2022 03:23:41 +0100 Subject: [PATCH 2/2] restored behaviour for COLUMNS --- pandas/core/reshape/pivot.py | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 22f8178570656..52c9a8b78a328 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -203,32 +203,12 @@ def __internal_pivot_table( to_unstack.append(name) table = agged.unstack(to_unstack) - # 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 not dropna: + if isinstance(table.columns, MultiIndex): + m = MultiIndex.from_arrays( + cartesian_product(table.columns.levels), names=table.columns.names + ) + table = table.reindex(m, axis=1) if isinstance(table, ABCDataFrame): table = table.sort_index(axis=1)