Skip to content

Commit 08d26c6

Browse files
committed
Fixup for pivot
1 parent 0849e1c commit 08d26c6

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

pandas/core/reshape/pivot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def _generate_marginal_results_without_values(
441441
margins_name: Hashable = "All",
442442
):
443443
margin_keys: list | Index
444-
if len(cols) > 0:
444+
if len(table.columns) > 0:
445445
# need to "interleave" the margins
446446
margin_keys = []
447447

pandas/tests/reshape/test_pivot.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,15 +1214,35 @@ def test_margins_no_values_two_rows(self, data):
12141214
result = data[["A", "B", "C"]].pivot_table(
12151215
index=["A", "B"], columns="C", aggfunc=len, margins=True
12161216
)
1217-
assert result.All.tolist() == [3.0, 1.0, 4.0, 3.0, 11.0]
1217+
expected = DataFrame(
1218+
index=MultiIndex(
1219+
levels=[["bar", "foo", "All"], ["one", "two", ""]],
1220+
codes=[[0, 0, 1, 1, 2], [0, 1, 0, 1, 2]],
1221+
names=["A", "B"],
1222+
),
1223+
columns=MultiIndex(
1224+
levels=[[], ["dull", "shiny"]],
1225+
codes=[[], []],
1226+
names=[None, "C"],
1227+
),
1228+
)
1229+
tm.assert_frame_equal(result, expected)
12181230

12191231
def test_margins_no_values_one_row_one_col(self, data):
12201232
# Regression test on pivot table: no values passed but row and col
12211233
# defined
12221234
result = data[["A", "B"]].pivot_table(
12231235
index="A", columns="B", aggfunc=len, margins=True
12241236
)
1225-
assert result.All.tolist() == [4.0, 7.0, 11.0]
1237+
expected = DataFrame(
1238+
index=Index(["bar", "foo", "All"], name="A"),
1239+
columns=MultiIndex(
1240+
levels=[[], ["dull", "shiny"]],
1241+
codes=[[], []],
1242+
names=[None, "B"],
1243+
),
1244+
)
1245+
tm.assert_frame_equal(result, expected)
12261246

12271247
def test_margins_no_values_two_row_two_cols(self, data):
12281248
# Regression test on pivot table: no values passed but rows and cols
@@ -1231,7 +1251,19 @@ def test_margins_no_values_two_row_two_cols(self, data):
12311251
result = data[["A", "B", "C", "D"]].pivot_table(
12321252
index=["A", "B"], columns=["C", "D"], aggfunc=len, margins=True
12331253
)
1234-
assert result.All.tolist() == [3.0, 1.0, 4.0, 3.0, 11.0]
1254+
expected = DataFrame(
1255+
index=MultiIndex(
1256+
levels=[["bar", "foo", "All"], ["one", "two", ""]],
1257+
codes=[[0, 0, 1, 1, 2], [0, 1, 0, 1, 2]],
1258+
names=["A", "B"],
1259+
),
1260+
columns=MultiIndex(
1261+
levels=[[], ["dull", "shiny"], list("abcdefghijk")],
1262+
codes=[[], [], []],
1263+
names=[None, "C", "D"],
1264+
),
1265+
)
1266+
tm.assert_frame_equal(result, expected)
12351267

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

0 commit comments

Comments
 (0)