8000 BUG: groupby.agg should always agg by rhshadrach · Pull Request #57706 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: groupby.agg should always agg #57706

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixup for pivot
  • Loading branch information
rhshadrach committed Mar 5, 2024
commit 08d26c6d1dabaab11115e329009f1490a8bfd292
2 changes: 1 addition & 1 deletion pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def _generate_marginal_results_without_values(
margins_name: Hashable = "All",
):
margin_keys: list | Index
if len(cols) > 0:
if len(table.columns) > 0:
# need to "interleave" the margins
margin_keys = []

Expand Down
38 changes: 35 additions & 3 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1214,15 +1214,35 @@ def test_margins_no_values_two_rows(self, data):
result = data[["A", "B", "C"]].pivot_table(
index=["A", "B"], columns="C", aggfunc=len, margins=True
)
assert result.All.tolist() == [3.0, 1.0, 4.0, 3.0, 11.0]
expected = DataFrame(
index=MultiIndex(
levels=[["bar", "foo", "All"], ["one", "two", ""]],
codes=[[0, 0, 1, 1, 2], [0, 1, 0, 1, 2]],
names=["A", "B"],
),
columns=MultiIndex(
levels=[[], ["dull", "shiny"]],
codes=[[], []],
names=[None, "C"],
),
)
tm.assert_frame_equal(result, expected)

def test_margins_no_values_one_row_one_col(self, data):
# Regression test on pivot table: no values passed but row and col
# defined
result = data[["A", "B"]].pivot_table(
index="A", columns="B", aggfunc=len, margins=True
)
assert result.All.tolist() == [4.0, 7.0, 11.0]
expected = DataFrame(
index=Index(["bar", "foo", "All"], name="A"),
columns=MultiIndex(
levels=[[], ["dull", "shiny"]],
codes=[[], []],
names=[None, "B"],
),
)
tm.assert_frame_equal(result, expected)

def test_margins_no_values_two_row_two_cols(self, data):
# Regression test on pivot table: no values passed but rows and cols
Expand All @@ -1231,7 +1251,19 @@ def test_margins_no_values_two_row_two_cols(self, data):
result = data[["A", "B", "C", "D"]].pivot_table(
index=["A", "B"], columns=["C", "D"], aggfunc=len, margins=True
)
assert result.All.tolist() == [3.0, 1.0, 4.0, 3.0, 11.0]
expected = DataFrame(
index=MultiIndex(
levels=[["bar", "foo", "All"], ["one", "two", ""]],
codes=[[0, 0, 1, 1, 2], [0, 1, 0, 1, 2]],
names=["A", "B"],
),
columns=MultiIndex(
levels=[[], ["dull", "shiny"], list("abcdefghijk")],
codes=[[], [], []],
names=[None, "C", "D"],
),
)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("margin_name", ["foo", "one", 666, None, ["a", "b"]])
def test_pivot_table_with_margins_set_margin_name(self, margin_name, data):
Expand Down
0