Skip to content

BUG: Fix inconsistent pivot table subaggregation when index is None #59629

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

Merged
merged 3 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -665,6 +665,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)