Skip to content

BUG: pivot_table with margins=True and numeric columns is buggy #42591

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 2 commits into from
Jul 22, 2021
Merged
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
17 changes: 17 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,23 @@ def test_pivot_table_sort_false(self):
)
tm.assert_frame_equal(result, expected)

def test_pivot_table_with_margins_and_numeric_columns(self):
# GH 26568
df = DataFrame([["a", "x", 1], ["a", "y", 2], ["b", "y", 3], ["b", "z", 4]])
df.columns = [10, 20, 30]

result = df.pivot_table(
index=10, columns=20, values=30, aggfunc="sum", fill_value=0, margins=True
)

expected = DataFrame([[1, 2, 0, 3], [0, 3, 4, 7], [1, 5, 4, 10]])
expected.columns = ["x", "y", "z", "All"]
expected.index = ["a", "b", "All"]
expected.columns.name = 20
expected.index.name = 10

tm.assert_frame_equal(result, expected)


class TestPivot:
def test_pivot(self):
Expand Down