From c21d8de4e6e28676eaca4a2486b973b46ff2b98e Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Tue, 1 Nov 2022 18:15:31 +0530 Subject: [PATCH 1/6] BUG: pivot_table with margins=T raises when results in empty df --- doc/source/whatsnew/v2.0.0.rst | 2 +- pandas/core/reshape/pivot.py | 8 ++++++-- pandas/tests/reshape/test_pivot.py | 7 +++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 0ab75355291f6..953459f2dd0d6 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -437,7 +437,7 @@ Reshaping - Bug in :meth:`DataFrame.unstack` and :meth:`Series.unstack` unstacking wrong level of :class:`MultiIndex` when :class:`MultiIndex` has mixed names (:issue:`48763`) - Bug in :meth:`DataFrame.pivot` not respecting ``None`` as column name (:issue:`48293`) - Bug in :func:`join` when ``left_on`` or ``right_on`` is or includes a :class:`CategoricalIndex` incorrectly raising ``AttributeError`` (:issue:`48464`) -- +- Bug in :meth:`DataFrame.pivot_table` raising ``ValueError`` with parameter ``margins=True`` when result is an empty DataFrame (:issue:`49240`) Sparse ^^^^^^ diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 8b49d681379c6..3f98f17ba4609 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -411,9 +411,13 @@ def _all_key(key): table_pieces.append(transformed_piece) margin_keys.append(all_key) - result = concat(table_pieces, axis=cat_axis) + try: + result = concat(table_pieces, axis=cat_axis) + except ValueError: + # GH 49240 + result = table - if len(rows) == 0: + if len(rows) == 0 or len(data) == 0: return result else: result = table diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index b021b1aa97a0e..db2db3655e155 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -2086,7 +2086,8 @@ def test_pivot_table_aggfunc_scalar_dropna(self, dropna): tm.assert_frame_equal(result, expected) - def test_pivot_table_empty_aggfunc(self): + @pytest.mark.parametrize("margins", [True, False]) + def test_pivot_table_empty_aggfunc(self, margins): # GH 9186 & GH 13483 df = DataFrame( { @@ -2096,7 +2097,9 @@ def test_pivot_table_empty_aggfunc(self): "D": [None, None, None, None, None], } ) - result = df.pivot_table(index="A", columns="D", values="id", aggfunc=np.size) + result = df.pivot_table( + index="A", columns="D", values="id", aggfunc=np.size, margins=margins + ) expected = DataFrame(index=Index([], dtype="int64", name="A")) expected.columns.name = "D" tm.assert_frame_equal(result, expected) From b0c3d5e48cc32895af5bd00cbf22e26a903e5ef8 Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Tue, 1 Nov 2022 21:42:08 +0530 Subject: [PATCH 2/6] suggested change --- pandas/core/reshape/pivot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 3f98f17ba4609..bfe6d6e3edaeb 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -411,11 +411,11 @@ def _all_key(key): table_pieces.append(transformed_piece) margin_keys.append(all_key) - try: - result = concat(table_pieces, axis=cat_axis) - except ValueError: + if table.empty: # GH 49240 - result = table + return table + else: + result = concat(table_pieces, axis=cat_axis) if len(rows) == 0 or len(data) == 0: return result From 9386878d8b71266d3becc12d68a07c36f557900c Mon Sep 17 00:00:00 2001 From: debnathshoham Date: Tue, 1 Nov 2022 23:01:00 +0530 Subject: [PATCH 3/6] suggested change --- pandas/core/reshape/pivot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index bfe6d6e3edaeb..e0d7ca38e7169 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -411,7 +411,7 @@ def _all_key(key): table_pieces.append(transformed_piece) margin_keys.append(all_key) - if table.empty: + if not table_pieces: # GH 49240 return table else: From d982d61353ec733c10ba02b30774b21e45c1c955 Mon Sep 17 00:00:00 2001 From: Shoham Debnath Date: Tue, 1 Nov 2022 23:13:20 +0530 Subject: [PATCH 4/6] Update pivot.py --- pandas/core/reshape/pivot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index e0d7ca38e7169..37e78c7dbf7a2 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -417,7 +417,7 @@ def _all_key(key): else: result = concat(table_pieces, axis=cat_axis) - if len(rows) == 0 or len(data) == 0: + if len(rows) == 0: return result else: result = table From 79bf88f515ba6f2035d03ad8e1a610f29fb7f4c6 Mon Sep 17 00:00:00 2001 From: Shoham Debnath Date: Tue, 1 Nov 2022 23:15:42 +0530 Subject: [PATCH 5/6] linked test to issue --- pandas/tests/reshape/test_pivot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index db2db3655e155..39ca2c1b75589 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -2088,7 +2088,7 @@ def test_pivot_table_aggfunc_scalar_dropna(self, dropna): @pytest.mark.parametrize("margins", [True, False]) def test_pivot_table_empty_aggfunc(self, margins): - # GH 9186 & GH 13483 + # GH 9186 & GH 13483 & GH 49240 df = DataFrame( { "A": [2, 2, 3, 3, 2], From a579234503b0c6dad4442903b341f7c317d645a3 Mon Sep 17 00:00:00 2001 From: Shoham Debnath Date: Tue, 1 Nov 2022 23:20:07 +0530 Subject: [PATCH 6/6] suggested edit --- doc/source/whatsnew/v2.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 9c7a3ec805f90..6c15c056fc7ce 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -441,7 +441,7 @@ Reshaping - Bug in :meth:`DataFrame.unstack` and :meth:`Series.unstack` unstacking wrong level of :class:`MultiIndex` when :class:`MultiIndex` has mixed names (:issue:`48763`) - Bug in :meth:`DataFrame.pivot` not respecting ``None`` as column name (:issue:`48293`) - Bug in :func:`join` when ``left_on`` or ``right_on`` is or includes a :class:`CategoricalIndex` incorrectly raising ``AttributeError`` (:issue:`48464`) -- Bug in :meth:`DataFrame.pivot_table` raising ``ValueError`` with parameter ``margins=True`` when result is an empty DataFrame (:issue:`49240`) +- Bug in :meth:`DataFrame.pivot_table` raising ``ValueError`` with parameter ``margins=True`` when result is an empty :class:`DataFrame` (:issue:`49240`) Sparse ^^^^^^