@@ -872,23 +872,26 @@ def test_union_categorical(self):
872
872
# new categories ordered by appearance
873
873
s = Categorical (['x' , 'y' , 'z' ])
874
874
s2 = Categorical (['a' , 'b' , 'c' ])
875
- result = union_categoricals ([s , s2 ]).categories
876
- expected = Index (['x' , 'y' , 'z' , 'a' , 'b' , 'c' ])
877
- tm .assert_index_equal (result , expected )
875
+ result = union_categoricals ([s , s2 ])
876
+ expected = Categorical (['x' , 'y' , 'z' , 'a' , 'b' , 'c' ],
877
+ categories = ['x' , 'y' , 'z' , 'a' , 'b' , 'c' ])
878
+ tm .assert_categorical_equal (result , expected )
878
879
879
- # can't be ordered
880
880
s = Categorical ([0 , 1.2 , 2 ], ordered = True )
881
881
s2 = Categorical ([0 , 1.2 , 2 ], ordered = True )
882
- with tm .assertRaises (TypeError ):
883
- union_categoricals ([s , s2 ])
882
+ result = union_categoricals ([s , s2 ])
883
+ expected = Categorical ([0 , 1.2 , 2 , 0 , 1.2 , 2 ], ordered = True )
884
+ tm .assert_categorical_equal (result , expected )
884
885
885
886
# must exactly match types
886
887
s = Categorical ([0 , 1.2 , 2 ])
887
888
s2 = Categorical ([2 , 3 , 4 ])
888
- with tm .assertRaises (TypeError ):
889
+ msg = 'dtype of categories must be the same'
890
+ with tm .assertRaisesRegexp (TypeError , msg ):
889
891
union_categoricals ([s , s2 ])
890
892
891
- with tm .assertRaises (ValueError ):
893
+ msg = 'No Categoricals to union'
894
+ with tm .assertRaisesRegexp (ValueError , msg ):
892
895
union_categoricals ([])
893
896
894
897
def test_union_categoricals_nan (self ):
@@ -944,6 +947,48 @@ def test_union_categoricals_empty(self):
944
947
pd .Categorical ([])])
945
948
tm .assert_categorical_equal (res , nanc )
946
949
950
+ def test_union_categorical_same_category (self ):
951
+ # check fastpath
952
+ c1 = Categorical ([1 , 2 , 3 , 4 ], categories = [1 , 2 , 3 , 4 ])
953
+ c2 = Categorical ([3 , 2 , 1 , np .nan ], categories = [1 , 2 , 3 , 4 ])
954
+ res = union_categoricals ([c1 , c2 ])
955
+ exp = Categorical ([1 , 2 , 3 , 4 , 3 , 2 , 1 , np .nan ],
956
+ categories = [1 , 2 , 3 , 4 ])
957
+ tm .assert_categorical_equal (res , exp )
958
+
959
+ c1 = Categorical (['z' , 'z' , 'z' ], categories = ['x' , 'y' , 'z' ])
960
+ c2 = Categorical (['x' , 'x' , 'x' ], categories = ['x' , 'y' , 'z' ])
961
+ res = union_categoricals ([c1 , c2 ])
962
+ exp = Categorical (['z' , 'z' , 'z' , 'x' , 'x' , 'x' ],
963
+ categories = ['x' , 'y' , 'z' ])
964
+ tm .assert_categorical_equal (res , exp )
965
+
966
+ def test_union_categoricals_ordered (self ):
967
+ c1 = Categorical ([1 , 2 , 3 ], ordered = True )
968
+ c2 = Categorical ([1 , 2 , 3 ], ordered = False )
969
+
970
+ msg = 'Categorical.ordered must be the same'
971
+ with tm .assertRaisesRegexp (TypeError , msg ):
972
+ union_categoricals ([c1 , c2 ])
973
+
974
+ res = union_categoricals ([c1 , c1 ])
975
+ exp = Categorical ([1 , 2 , 3 , 1 , 2 , 3 ], ordered = True )
976
+ tm .assert_categorical_equal (res , exp )
977
+
978
+ c1 = Categorical ([1 , 2 , 3 , np .nan ], ordered = True )
979
+ c2 = Categorical ([3 , 2 ], categories = [1 , 2 , 3 ], ordered = True )
980
+
981
+ res = union_categoricals ([c1 , c2 ])
982
+ exp = Categorical ([1 , 2 , 3 , np .nan , 3 , 2 ], ordered = True )
983
+ tm .assert_categorical_equal (res , exp )
984
+
985
+ c1 = Categorical ([1 , 2 , 3 ], ordered = True )
986
+ c2 = Categorical ([1 , 2 , 3 ], categories = [3 , 2 , 1 ], ordered = True )
987
+
988
+ msg = "to union ordered Categoricals, all categories must be the same"
989
+ with tm .assertRaisesRegexp (TypeError , msg ):
990
+ union_categoricals ([c1 , c2 ])
991
+
947
992
def test_concat_bug_1719 (self ):
948
993
ts1 = tm .makeTimeSeries ()
949
994
ts2 = tm .makeTimeSeries ()[::2 ]
0 commit comments