Skip to content

Commit

Permalink
BUG: Fix inconsistent pivot table subaggregation when index is None (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
matiaslindgren authored Sep 7, 2024
1 parent 352289b commit 80b6850
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ Reshaping
- 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`)
- Bug in :meth:`DataFrame.merge` where merging on a column containing only ``NaN`` values resulted in an out-of-bounds array access (:issue:`59421`)
- Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`)
- Bug in :meth:`DataFrame.pivot_table` incorrectly subaggregating results when called without an ``index`` argument (:issue:`58722`)
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)

Sparse
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,12 @@ def _all_key(key):
table_pieces.append(piece)
margin_keys.append(all_key)
else:
from pandas import DataFrame
margin = (
data[cols[:1] + values]
.groupby(cols[:1], observed=observed)
.agg(aggfunc, **kwargs)
.T
)

cat_axis = 0
for key, piece in table.groupby(level=0, observed=observed):
Expand All @@ -566,9 +571,7 @@ def _all_key(key):
else:
all_key = margins_name
table_pieces.append(piece)
# GH31016 this is to calculate margin for each group, and assign
# corresponded key as index
transformed_piece = DataFrame(piece.apply(aggfunc, **kwargs)).T
transformed_piece = margin[key].to_frame().T
if isinstance(piece.index, MultiIndex):
# We are adding an empty level
transformed_piece.index = MultiIndex.from_tuples(
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2785,3 +2785,31 @@ def test_pivot_empty_with_datetime(self):
index="category", columns="value", values="timestamp"
)
assert df_pivoted.empty

def test_pivot_margins_with_none_index(self):
# GH#58722
df = DataFrame(
{
"x": [1, 1, 2],
"y": [3, 3, 4],
"z": [5, 5, 6],
"w": [7, 8, 9],
}
)
result = df.pivot_table(
index=None,
columns=["y", "z"],
values="w",
margins=True,
aggfunc="count",
)
expected = DataFrame(
[[2, 2, 1, 1]],
index=["w"],
columns=MultiIndex(
levels=[[3, 4], [5, 6, "All"]],
codes=[[0, 0, 1, 1], [0, 2, 1, 2]],
names=["y", "z"],
),
)
tm.assert_frame_equal(result, expected)

0 comments on commit 80b6850

Please sign in to comment.