Skip to content

Commit 2d39e86

Browse files
committed
Fixes #24893: added test and run black
1 parent b81149b commit 2d39e86

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

pandas/core/reshape/pivot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,9 @@ def _add_margins(
254254
try:
255255
for dtype in set(result.dtypes):
256256
cols = result.select_dtypes([dtype]).columns
257-
margin_dummy[cols] = margin_dummy[cols].apply(maybe_downcast_to_dtype,
258-
args=(dtype,))
257+
margin_dummy[cols] = margin_dummy[cols].apply(
258+
maybe_downcast_to_dtype, args=(dtype,)
259+
)
259260
result = result.append(margin_dummy)
260261
except TypeError:
261262

pandas/tests/reshape/test_pivot.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,6 @@ def test_pivot_table_margins_name_with_aggfunc_list(self):
16011601
expected = pd.DataFrame(table.values, index=ix, columns=cols)
16021602
tm.assert_frame_equal(table, expected)
16031603

1604-
@pytest.mark.xfail(reason="GH#17035 (np.mean of ints is casted back to ints)")
16051604
def test_categorical_margins(self, observed):
16061605
# GH 10989
16071606
df = pd.DataFrame(
@@ -1615,6 +1614,24 @@ def test_categorical_margins(self, observed):
16151614
table = df.pivot_table("x", "y", "z", dropna=observed, margins=True)
16161615
tm.assert_frame_equal(table, expected)
16171616

1617+
def test_margins_casted_to_float(self):
1618+
# GH #24893
1619+
df = pd.DataFrame(
1620+
{
1621+
"A": [2, 4, 6, 8],
1622+
"B": [1, 4, 5, 8],
1623+
"C": [1, 3, 4, 6],
1624+
"D": ["X", "X", "Y", "Y"],
1625+
}
1626+
)
1627+
1628+
result = pd.pivot_table(df, index="D", margins=True)
1629+
expected = pd.DataFrame(
1630+
{"A": [3, 7, 5], "B": [2.5, 6.5, 4.5], "C": [2, 5, 3.5]},
1631+
index=pd.Index(["X", "Y", "All"], name="D"),
1632+
)
1633+
tm.assert_frame_equal(result, expected)
1634+
16181635
@pytest.mark.xfail(reason="GH#17035 (np.mean of ints is casted back to ints)")
16191636
def test_categorical_margins_category(self, observed):
16201637
df = pd.DataFrame(

0 commit comments

Comments
 (0)