@@ -719,6 +719,26 @@ def test_crosstab_dropna(self):
719
719
('two' , 'dull' ), ('two' , 'shiny' )])
720
720
assert_equal (res .columns .values , m .values )
721
721
722
+ def test_categorical_margins (self ):
723
+ # GH 10989
724
+ df = pd .DataFrame ({'x' : np .arange (8 ),
725
+ 'y' : np .arange (8 ) // 4 ,
726
+ 'z' : np .arange (8 ) % 2 })
727
+
728
+ expected = pd .DataFrame ([[1.0 , 2.0 , 1.5 ],[5 , 6 , 5.5 ],[3 , 4 , 3.5 ]])
729
+ expected .index = Index ([0 ,1 ,'All' ],name = 'y' )
730
+ expected .columns = Index ([0 ,1 ,'All' ],name = 'z' )
731
+
732
+ data = df .copy ()
733
+ table = data .pivot_table ('x' , 'y' , 'z' , margins = True )
734
+ tm .assert_frame_equal (table , expected )
735
+
736
+ data = df .copy ()
737
+ data .y = data .y .astype ('category' )
738
+ data .z = data .z .astype ('category' )
739
+ table = data .pivot_table ('x' , 'y' , 'z' , margins = True )
740
+ tm .assert_frame_equal (table , expected )
741
+
722
742
if __name__ == '__main__' :
723
743
import nose
724
744
nose .runmodule (argv = [__file__ , '-vvs' , '-x' , '--pdb' , '--pdb-failure' ],
0 commit comments