@@ -459,6 +459,41 @@ def _check_output(result, values_col, index=['A', 'B'],
459459
460460 tm .assert_frame_equal (result ['SALARY' ], expected ['SALARY' ])
461461
462+ def test_margins_dtype (self ):
463+ # GH 17013
464+
465+ df = self .data .copy ()
466+ df [['D' , 'E' , 'F' ]] = np .arange (len (df ) * 3 ).reshape (len (df ), 3 )
467+
468+ mi_val = list (product (['bar' , 'foo' ], ['one' , 'two' ])) + [('All' , '' )]
469+ mi = MultiIndex .from_tuples (mi_val , names = ('A' , 'B' ))
470+ expected = DataFrame ({'dull' : [12 , 21 , 3 , 9 , 45 ],
471+ 'shiny' : [33 , 0 , 36 , 51 , 120 ]},
472+ index = mi ).rename_axis ('C' , axis = 1 )
473+ expected ['All' ] = expected ['dull' ] + expected ['shiny' ]
474+
475+ result = df .pivot_table (values = 'D' , index = ['A' , 'B' ],
476+ columns = 'C' , margins = True ,
477+ aggfunc = np .sum , fill_value = 0 )
478+
479+ tm .assert_frame_equal (expected , result )
480+
481+ @pytest .mark .xfail (reason = 'GH 17035 (len of floats is casted back to '
482+ 'floats)' )
483+ def test_margins_dtype_len (self ):
484+ mi_val = list (product (['bar' , 'foo' ], ['one' , 'two' ])) + [('All' , '' )]
485+ mi = MultiIndex .from_tuples (mi_val , names = ('A' , 'B' ))
486+ expected = DataFrame ({'dull' : [1 , 1 , 2 , 1 , 5 ],
487+ 'shiny' : [2 , 0 , 2 , 2 , 6 ]},
488+ index = mi ).rename_axis ('C' , axis = 1 )
489+ expected ['All' ] = expected ['dull' ] + expected ['shiny' ]
490+
491+ result = self .data .pivot_table (values = 'D' , index = ['A' , 'B' ],
492+ columns = 'C' , margins = True ,
493+ aggfunc = len , fill_value = 0 )
494+
495+ tm .assert_frame_equal (expected , result )
496+
462497 def test_pivot_integer_columns (self ):
463498 # caused by upstream bug in unstack
464499
@@ -894,6 +929,8 @@ def test_pivot_table_margins_name_with_aggfunc_list(self):
894929 expected = pd .DataFrame (table .values , index = ix , columns = cols )
895930 tm .assert_frame_equal (table , expected )
896931
932+ @pytest .mark .xfail (reason = 'GH 17035 (np.mean of ints is casted back to '
933+ 'ints)' )
897934 def test_categorical_margins (self ):
898935 # GH 10989
899936 df = pd .DataFrame ({'x' : np .arange (8 ),
@@ -904,14 +941,23 @@ def test_categorical_margins(self):
904941 expected .index = Index ([0 , 1 , 'All' ], name = 'y' )
905942 expected .columns = Index ([0 , 1 , 'All' ], name = 'z' )
906943
907- data = df .copy ()
908- table = data .pivot_table ('x' , 'y' , 'z' , margins = True )
944+ table = df .pivot_table ('x' , 'y' , 'z' , margins = True )
909945 tm .assert_frame_equal (table , expected )
910946
911- data = df .copy ()
912- data .y = data .y .astype ('category' )
913- data .z = data .z .astype ('category' )
914- table = data .pivot_table ('x' , 'y' , 'z' , margins = True )
947+ @pytest .mark .xfail (reason = 'GH 17035 (np.mean of ints is casted back to '
948+ 'ints)' )
949+ def test_categorical_margins_category (self ):
950+ df = pd .DataFrame ({'x' : np .arange (8 ),
951+ 'y' : np .arange (8 ) // 4 ,
952+ 'z' : np .arange (8 ) % 2 })
953+
954+ expected = pd .DataFrame ([[1.0 , 2.0 , 1.5 ], [5 , 6 , 5.5 ], [3 , 4 , 3.5 ]])
955+ expected .index = Index ([0 , 1 , 'All' ], name = 'y' )
956+ expected .columns = Index ([0 , 1 , 'All' ], name = 'z' )
957+
958+ df .y = df .y .astype ('category' )
959+ df .z = df .z .astype ('category' )
960+ table = df .pivot_table ('x' , 'y' , 'z' , margins = True )
915961 tm .assert_frame_equal (table , expected )
916962
917963 def test_categorical_aggfunc (self ):
0 commit comments