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