@@ -746,6 +746,7 @@ def test_margin_normalize_multiple_columns(self):
746
746
747
747
748
748
def test_categoricals ():
749
+ # https://github.com/pandas-dev/pandas/issues/37465
749
750
g = np .random .RandomState (25982704 )
750
751
a = Series (g .randint (0 , 3 , size = 100 )).astype ("category" )
751
752
b = Series (g .randint (0 , 2 , size = 100 )).astype ("category" )
@@ -755,3 +756,14 @@ def test_categoricals():
755
756
values = [[18 , 16 , 34 ], [18 , 16 , 34 ], [16 , 16 , 32 ], [52 , 48 , 100 ]]
756
757
expected = DataFrame (values , index , columns )
757
758
tm .assert_frame_equal (result , expected )
759
+
760
+ # Verify when categorical does not have all values present
761
+ g = np .random .RandomState (25982704 )
762
+ a = Series (g .randint (0 , 3 , size = 100 )).astype ("category" )
763
+ a .loc [a == 1 ] = 2
764
+ assert a .value_counts ().loc [1 ] == 0
765
+ b = Series (g .randint (0 , 2 , size = 100 )).astype ("category" )
766
+ result = crosstab (a , b , margins = True , dropna = False )
767
+ values = [[18 , 16 , 34 ], [0 , 0 , np .nan ], [34 , 32 , 66 ], [52 , 48 , 100 ]]
768
+ expected = DataFrame (values , index , columns )
769
+ tm .assert_frame_equal (result , expected )
0 commit comments