Skip to content

Commit 80b6850

Browse files
BUG: Fix inconsistent pivot table subaggregation when index is None (#59629)
1 parent 352289b commit 80b6850

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ Reshaping
668668
- Bug in :meth:`DataFrame.join` when a :class:`DataFrame` with a :class:`MultiIndex` would raise an ``AssertionError`` when :attr:`MultiIndex.names` contained ``None``. (:issue:`58721`)
669669
- Bug in :meth:`DataFrame.merge` where merging on a column containing only ``NaN`` values resulted in an out-of-bounds array access (:issue:`59421`)
670670
- Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`)
671+
- Bug in :meth:`DataFrame.pivot_table` incorrectly subaggregating results when called without an ``index`` argument (:issue:`58722`)
671672
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)
672673

673674
Sparse

pandas/core/reshape/pivot.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,12 @@ def _all_key(key):
557557
table_pieces.append(piece)
558558
margin_keys.append(all_key)
559559
else:
560-
from pandas import DataFrame
560+
margin = (
561+
data[cols[:1] + values]
562+
.groupby(cols[:1], observed=observed)
563+
.agg(aggfunc, **kwargs)
564+
.T
565+
)
561566

562567
cat_axis = 0
563568
for key, piece in table.groupby(level=0, observed=observed):
@@ -566,9 +571,7 @@ def _all_key(key):
566571
else:
567572
all_key = margins_name
568573
table_pieces.append(piece)
569-
# GH31016 this is to calculate margin for each group, and assign
570-
# corresponded key as index
571-
transformed_piece = DataFrame(piece.apply(aggfunc, **kwargs)).T
574+
transformed_piece = margin[key].to_frame().T
572575
if isinstance(piece.index, MultiIndex):
573576
# We are adding an empty level
574577
transformed_piece.index = MultiIndex.from_tuples(

pandas/tests/reshape/test_pivot.py

+28
Original file line numberDiff line numberDiff line change
@@ -2785,3 +2785,31 @@ def test_pivot_empty_with_datetime(self):
27852785
index="category", columns="value", values="timestamp"
27862786
)
27872787
assert df_pivoted.empty
2788+
2789+
def test_pivot_margins_with_none_index(self):
2790+
# GH#58722
2791+
df = DataFrame(
2792+
{
2793+
"x": [1, 1, 2],
2794+
"y": [3, 3, 4],
2795+
"z": [5, 5, 6],
2796+
"w": [7, 8, 9],
2797+
}
2798+
)
2799+
result = df.pivot_table(
2800+
index=None,
2801+
columns=["y", "z"],
2802+
values="w",
2803+
margins=True,
2804+
aggfunc="count",
2805+
)
2806+
expected = DataFrame(
2807+
[[2, 2, 1, 1]],
2808+
index=["w"],
2809+
columns=MultiIndex(
2810+
levels=[[3, 4], [5, 6, "All"]],
2811+
codes=[[0, 0, 1, 1], [0, 2, 1, 2]],
2812+
names=["y", "z"],
2813+
),
2814+
)
2815+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)