@@ -818,6 +818,27 @@ def test_margin_support_Float(self):
818
818
)
819
819
tm .assert_frame_equal (result , expected )
820
820
821
+ def test_margin_with_ordered_categorical_column (self ):
822
+ # GH 25278
823
+ df = DataFrame (
824
+ {
825
+ "First" : ["B" , "B" , "C" , "A" , "B" , "C" ],
826
+ "Second" : ["C" , "B" , "B" , "B" , "C" , "A" ],
827
+ }
828
+ )
829
+ df ["First" ] = df ["First" ].astype (CategoricalDtype (ordered = True ))
830
+ customized_categories_order = ["C" , "A" , "B" ]
831
+ df ["First" ] = df ["First" ].cat .reorder_categories (customized_categories_order )
832
+ result = crosstab (df ["First" ], df ["Second" ], margins = True )
833
+
834
+ expected_index = Index (["C" , "A" , "B" , "All" ], name = "First" )
835
+ expected_columns = Index (["A" , "B" , "C" , "All" ], name = "Second" )
836
+ expected_data = [[1 , 1 , 0 , 2 ], [0 , 1 , 0 , 1 ], [0 , 1 , 2 , 3 ], [1 , 3 , 2 , 6 ]]
837
+ expected = DataFrame (
838
+ expected_data , index = expected_index , columns = expected_columns
839
+ )
840
+ tm .assert_frame_equal (result , expected )
841
+
821
842
822
843
@pytest .mark .parametrize ("a_dtype" , ["category" , "int64" ])
823
844
@pytest .mark .parametrize ("b_dtype" , ["category" , "int64" ])
0 commit comments