@@ -1630,6 +1630,25 @@ def test_categorical_margins_category(self, observed):
1630
1630
table = df .pivot_table ("x" , "y" , "z" , dropna = observed , margins = True )
1631
1631
tm .assert_frame_equal (table , expected )
1632
1632
1633
+ def test_margins_casted_to_float (self , observed ):
1634
+ # GH #24893
1635
+ df = pd .DataFrame (
1636
+ {
1637
+ "A" : [2 , 4 , 6 , 8 ],
1638
+ "B" : [1 , 4 , 5 , 8 ],
1639
+ "C" : [1 , 3 , 4 , 6 ],
1640
+ "D" : ["X" , "X" , "Y" , "Y" ],
1641
+ }
1642
+ )
1643
+
1644
+ result = pd .pivot_table (df , index = "D" , margins = True )
1645
+ expected = pd .DataFrame (
1646
+ {"A" : [3 , 7 , 5 ], "B" : [2.5 , 6.5 , 4.5 ], "C" : [2 , 5 , 3.5 ]},
1647
+ index = pd .Index (["X" , "Y" , "All" ], name = "D" ),
1648
+ )
1649
+ table = result
1650
+ tm .assert_frame_equal (result , expected )
1651
+
1633
1652
def test_categorical_aggfunc (self , observed ):
1634
1653
# GH 9534
1635
1654
df = pd .DataFrame (
@@ -2527,4 +2546,4 @@ def test_margin_normalize(self):
2527
2546
codes = [[1 , 1 , 2 , 2 , 0 ], [1 , 2 , 1 , 2 , 0 ]],
2528
2547
names = ["A" , "B" ],
2529
2548
)
2530
- tm .assert_frame_equal (result , expected )
2549
+ tm .assert_frame_equal (result , expected )
0 commit comments