@@ -115,7 +115,12 @@ def test_assigning_ops(self):
115
115
tm .assert_frame_equal (df , exp_single_cats_value )
116
116
117
117
# - assign a single value not in the current categories set
118
- with pytest .raises (ValueError ):
118
+ msg1 = (
119
+ "Cannot setitem on a Categorical with a new category, "
120
+ "set the categories first"
121
+ )
122
+ msg2 = "Cannot set a Categorical with another, without identical categories"
123
+ with pytest .raises (ValueError , match = msg1 ):
119
124
df = orig .copy ()
120
125
df .iloc [2 , 0 ] = "c"
121
126
@@ -125,7 +130,7 @@ def test_assigning_ops(self):
125
130
tm .assert_frame_equal (df , exp_single_row )
126
131
127
132
# - assign a complete row (mixed values) not in categories set
128
- with pytest .raises (ValueError ):
133
+ with pytest .raises (ValueError , match = msg1 ):
129
134
df = orig .copy ()
130
135
df .iloc [2 , :] = ["c" , 2 ]
131
136
@@ -134,7 +139,7 @@ def test_assigning_ops(self):
134
139
df .iloc [2 :4 , :] = [["b" , 2 ], ["b" , 2 ]]
135
140
tm .assert_frame_equal (df , exp_multi_row )
136
141
137
- with pytest .raises (ValueError ):
142
+ with pytest .raises (ValueError , match = msg1 ):
138
143
df = orig .copy ()
139
144
df .iloc [2 :4 , :] = [["c" , 2 ], ["c" , 2 ]]
140
145
@@ -144,12 +149,12 @@ def test_assigning_ops(self):
144
149
df .iloc [2 :4 , 0 ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
145
150
tm .assert_frame_equal (df , exp_parts_cats_col )
146
151
147
- with pytest .raises (ValueError ):
152
+ with pytest .raises (ValueError , match = msg2 ):
148
153
# different categories -> not sure if this should fail or pass
149
154
df = orig .copy ()
150
155
df .iloc [2 :4 , 0 ] = Categorical (list ("bb" ), categories = list ("abc" ))
151
156
152
- with pytest .raises (ValueError ):
157
+ with pytest .raises (ValueError , match = msg2 ):
153
158
# different values
154
159
df = orig .copy ()
155
160
df .iloc [2 :4 , 0 ] = Categorical (list ("cc" ), categories = list ("abc" ))
@@ -160,7 +165,7 @@ def test_assigning_ops(self):
160
165
df .iloc [2 :4 , 0 ] = ["b" , "b" ]
161
166
tm .assert_frame_equal (df , exp_parts_cats_col )
162
167
163
- with pytest .raises (ValueError ):
168
+ with pytest .raises (ValueError , match = msg1 ):
164
169
df .iloc [2 :4 , 0 ] = ["c" , "c" ]
165
170
166
171
# loc
@@ -175,7 +180,7 @@ def test_assigning_ops(self):
175
180
tm .assert_frame_equal (df , exp_single_cats_value )
176
181
177
182
# - assign a single value not in the current categories set
178
- with pytest .raises (ValueError ):
183
+ with pytest .raises (ValueError , match = msg1 ):
179
184
df = orig .copy ()
180
185
df .loc ["j" , "cats" ] = "c"
181
186
@@ -185,7 +190,7 @@ def test_assigning_ops(self):
185
190
tm .assert_frame_equal (df , exp_single_row )
186
191
187
192
# - assign a complete row (mixed values) not in categories set
188
- with pytest .raises (ValueError ):
193
+ with pytest .raises (ValueError , match = msg1 ):
189
194
df = orig .copy ()
190
195
df .loc ["j" , :] = ["c" , 2 ]
191
196
@@ -194,7 +199,7 @@ def test_assigning_ops(self):
194
199
df .loc ["j" :"k" , :] = [["b" , 2 ], ["b" , 2 ]]
195
200
tm .assert_frame_equal (df , exp_multi_row )
196
201
197
- with pytest .raises (ValueError ):
202
+ with pytest .raises (ValueError , match = msg1 ):
198
203
df = orig .copy ()
199
204
df .loc ["j" :"k" , :] = [["c" , 2 ], ["c" , 2 ]]
200
205
@@ -204,14 +209,14 @@ def test_assigning_ops(self):
204
209
df .loc ["j" :"k" , "cats" ] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
205
210
tm .assert_frame_equal (df , exp_parts_cats_col )
206
211
207
- with pytest .raises (ValueError ):
212
+ with pytest .raises (ValueError , match = msg2 ):
208
213
# different categories -> not sure if this should fail or pass
209
214
df = orig .copy ()
210
215
df .loc ["j" :"k" , "cats" ] = Categorical (
211
216
["b" , "b" ], categories = ["a" , "b" , "c" ]
212
217
)
213
218
214
- with pytest .raises (ValueError ):
219
+ with pytest .raises (ValueError , match = msg2 ):
215
220
# different values
216
221
df = orig .copy ()
217
222
df .loc ["j" :"k" , "cats" ] = Categorical (
@@ -224,7 +229,7 @@ def test_assigning_ops(self):
224
229
df .loc ["j" :"k" , "cats" ] = ["b" , "b" ]
225
230
tm .assert_frame_equal (df , exp_parts_cats_col )
226
231
227
- with pytest .raises (ValueError ):
232
+ with pytest .raises (ValueError , match = msg1 ):
228
233
df .loc ["j" :"k" , "cats" ] = ["c" , "c" ]
229
234
230
235
# loc
@@ -239,7 +244,7 @@ def test_assigning_ops(self):
239
244
tm .assert_frame_equal (df , exp_single_cats_value )
240
245
241
246
# - assign a single value not in the current categories set
242
- with pytest .raises (ValueError ):
247
+ with pytest .raises (ValueError , match = msg1 ):
243
248
df = orig .copy ()
244
249
df .loc ["j" , df .columns [0 ]] = "c"
245
250
@@ -249,7 +254,7 @@ def test_assigning_ops(self):
249
254
tm .assert_frame_equal (df , exp_single_row )
250
255
251
256
# - assign a complete row (mixed values) not in categories set
252
- with pytest .raises (ValueError ):
257
+ with pytest .raises (ValueError , match = msg1 ):
253
258
df = orig .copy ()
254
259
df .loc ["j" , :] = ["c" , 2 ]
255
260
@@ -258,7 +263,7 @@ def test_assigning_ops(self):
258
263
df .loc ["j" :"k" , :] = [["b" , 2 ], ["b" , 2 ]]
259
264
tm .assert_frame_equal (df , exp_multi_row )
260
265
261
- with pytest .raises (ValueError ):
266
+ with pytest .raises (ValueError , match = msg1 ):
262
267
df = orig .copy ()
263
268
df .loc ["j" :"k" , :] = [["c" , 2 ], ["c" , 2 ]]
264
269
@@ -268,14 +273,14 @@ def test_assigning_ops(self):
268
273
df .loc ["j" :"k" , df .columns [0 ]] = Categorical (["b" , "b" ], categories = ["a" , "b" ])
269
274
tm .assert_frame_equal (df , exp_parts_cats_col )
270
275
271
- with pytest .raises (ValueError ):
276
+ with pytest .raises (ValueError , match = msg2 ):
272
277
# different categories -> not sure if this should fail or pass
273
278
df = orig .copy ()
274
279
df .loc ["j" :"k" , df .columns [0 ]] = Categorical (
275
280
["b" , "b" ], categories = ["a" , "b" , "c" ]
276
281
)
277
282
278
- with pytest .raises (ValueError ):
283
+ with pytest .raises (ValueError , match = msg2 ):
279
284
# different values
280
285
df = orig .copy ()
281
286
df .loc ["j" :"k" , df .columns [0 ]] = Categorical (
@@ -288,7 +293,7 @@ def test_assigning_ops(self):
288
293
df .loc ["j" :"k" , df .columns [0 ]] = ["b" , "b" ]
289
294
tm .assert_frame_equal (df , exp_parts_cats_col )
290
295
291
- with pytest .raises (ValueError ):
296
+ with pytest .raises (ValueError , match = msg1 ):
292
297
df .loc ["j" :"k" , df .columns [0 ]] = ["c" , "c" ]
293
298
294
299
# iat
@@ -297,7 +302,7 @@ def test_assigning_ops(self):
297
302
tm .assert_frame_equal (df , exp_single_cats_value )
298
303
299
304
# - assign a single value not in the current categories set
300
- with pytest .raises (ValueError ):
305
+ with pytest .raises (ValueError , match = msg1 ):
301
306
df = orig .copy ()
302
307
df .iat [2 , 0 ] = "c"
303
308
@@ -308,7 +313,7 @@ def test_assigning_ops(self):
308
313
tm .assert_frame_equal (df , exp_single_cats_value )
309
314
310
315
# - assign a single value not in the current categories set
311
- with pytest .raises (ValueError ):
316
+ with pytest .raises (ValueError , match = msg1 ):
312
317
df = orig .copy ()
313
318
df .at ["j" , "cats" ] = "c"
314
319
@@ -332,7 +337,7 @@ def test_assigning_ops(self):
332
337
df .at ["j" , "cats" ] = "b"
333
338
tm .assert_frame_equal (df , exp_single_cats_value )
334
339
335
- with pytest .raises (ValueError ):
340
+ with pytest .raises (ValueError , match = msg1 ):
336
341
df = orig .copy ()
337
342
df .at ["j" , "cats" ] = "c"
338
343
0 commit comments