@@ -83,13 +83,15 @@ def test_rename_categories(self):
83
83
)
84
84
tm .assert_index_equal (cat .categories , Index ([1 , 2 , 3 ]))
85
85
86
- # Lengthen
87
- with pytest .raises (ValueError ):
88
- cat .rename_categories ([1 , 2 , 3 , 4 ])
89
-
90
- # Shorten
91
- with pytest .raises (ValueError ):
92
- cat .rename_categories ([1 , 2 ])
86
+ @pytest .mark .parametrize ("new_categories" , [[1 , 2 , 3 , 4 ], [1 , 2 ]])
87
+ def test_rename_categories_wrong_length_raises (self , new_categories ):
88
+ cat = Categorical (["a" , "b" , "c" , "a" ])
89
+ msg = (
90
+ "new categories need to have the same number of items as the"
91
+ " old categories!"
92
+ )
93
+ with pytest .raises (ValueError , match = msg ):
94
+ cat .rename_categories (new_categories )
93
95
94
96
def test_rename_categories_series (self ):
95
97
# https://github.com/pandas-dev/pandas/issues/17981
@@ -149,19 +151,19 @@ def test_reorder_categories(self):
149
151
assert res is None
150
152
tm .assert_categorical_equal (cat , new )
151
153
152
- # not all "old" included in "new"
154
+ @pytest .mark .parametrize (
155
+ "new_categories" ,
156
+ [
157
+ ["a" ], # not all "old" included in "new"
158
+ ["a" , "b" , "d" ], # still not all "old" in "new"
159
+ ["a" , "b" , "c" , "d" ], # all "old" included in "new", but too long
160
+ ],
161
+ )
162
+ def test_reorder_categories_raises (self , new_categories ):
153
163
cat = Categorical (["a" , "b" , "c" , "a" ], ordered = True )
154
-
155
- with pytest .raises (ValueError ):
156
- cat .reorder_categories (["a" ])
157
-
158
- # still not all "old" in "new"
159
- with pytest .raises (ValueError ):
160
- cat .reorder_categories (["a" , "b" , "d" ])
161
-
162
- # all "old" included in "new", but too long
163
- with pytest .raises (ValueError ):
164
- cat .reorder_categories (["a" , "b" , "c" , "d" ])
164
+ msg = "items in new_categories are not the same as in old categories"
165
+ with pytest .raises (ValueError , match = msg ):
166
+ cat .reorder_categories (new_categories )
165
167
166
168
def test_add_categories (self ):
167
169
cat = Categorical (["a" , "b" , "c" , "a" ], ordered = True )
@@ -184,10 +186,6 @@ def test_add_categories(self):
184
186
tm .assert_categorical_equal (cat , new )
185
187
assert res is None
186
188
187
- # new is in old categories
188
- with pytest .raises (ValueError ):
189
- cat .add_categories (["d" ])
190
-
191
189
# GH 9927
192
190
cat = Categorical (list ("abc" ), ordered = True )
193
191
expected = Categorical (list ("abc" ), categories = list ("abcde" ), ordered = True )
@@ -201,6 +199,13 @@ def test_add_categories(self):
201
199
res = cat .add_categories (["d" , "e" ])
202
200
tm .assert_categorical_equal (res , expected )
203
201
202
+ def test_add_categories_existing_raises (self ):
203
+ # new is in old categories
204
+ cat = Categorical (["a" , "b" , "c" , "d" ], ordered = True )
205
+ msg = re .escape ("new categories must not include old categories: {'d'}" )
206
+ with pytest .raises (ValueError , match = msg ):
207
+ cat .add_categories (["d" ])
208
+
204
209
def test_set_categories (self ):
205
210
cat = Categorical (["a" , "b" , "c" , "a" ], ordered = True )
206
211
exp_categories = Index (["c" , "b" , "a" ])
@@ -453,13 +458,13 @@ def test_codes_immutable(self):
453
458
tm .assert_numpy_array_equal (c .codes , exp )
454
459
455
460
# Assignments to codes should raise
456
- with pytest .raises (ValueError ):
461
+ with pytest .raises (ValueError , match = "cannot set Categorical codes directly" ):
457
462
c .codes = np .array ([0 , 1 , 2 , 0 , 1 ], dtype = "int8" )
458
463
459
464
# changes in the codes array should raise
460
465
codes = c .codes
461
466
462
- with pytest .raises (ValueError ):
467
+ with pytest .raises (ValueError , match = "assignment destination is read-only" ):
463
468
codes [4 ] = 1
464
469
465
470
# But even after getting the codes, the original array should still be
0 commit comments