@@ -49,10 +49,12 @@ def test_getitem_boolean_empty():
49
49
50
50
# invalid because of the boolean indexer
51
51
# that's empty or not-aligned
52
- with pytest .raises (IndexingError ):
52
+ msg = (r"Unalignable boolean Series provided as indexer \(index of"
53
+ r" the boolean Series and of the indexed object do not match" )
54
+ with pytest .raises (IndexingError , match = msg ):
53
55
s [Series ([], dtype = bool )]
54
56
55
- with pytest .raises (IndexingError ):
57
+ with pytest .raises (IndexingError , match = msg ):
56
58
s [Series ([True ], dtype = bool )]
57
59
58
60
@@ -77,8 +79,11 @@ def test_getitem_boolean_object(test_data):
77
79
78
80
# nans raise exception
79
81
omask [5 :10 ] = np .nan
80
- pytest .raises (Exception , s .__getitem__ , omask )
81
- pytest .raises (Exception , s .__setitem__ , omask , 5 )
82
+ msg = "cannot index with vector containing NA / NaN values"
83
+ with pytest .raises (ValueError , match = msg ):
84
+ s [omask ]
85
+ with pytest .raises (ValueError , match = msg ):
86
+ s [omask ] = 5
82
87
83
88
84
89
def test_getitem_setitem_boolean_corner (test_data ):
@@ -87,15 +92,17 @@ def test_getitem_setitem_boolean_corner(test_data):
87
92
88
93
# these used to raise...??
89
94
90
- pytest .raises (Exception , ts .__getitem__ , mask_shifted )
91
- pytest .raises (Exception , ts .__setitem__ , mask_shifted , 1 )
92
- # ts[mask_shifted]
93
- # ts[mask_shifted] = 1
95
+ msg = (r"Unalignable boolean Series provided as indexer \(index of"
96
+ r" the boolean Series and of the indexed object do not match" )
97
+ with pytest .raises (IndexingError , match = msg ):
98
+ ts [mask_shifted ]
99
+ with pytest .raises (IndexingError , match = msg ):
100
+ ts [mask_shifted ] = 1
94
101
95
- pytest .raises (Exception , ts . loc . __getitem__ , mask_shifted )
96
- pytest . raises ( Exception , ts .loc . __setitem__ , mask_shifted , 1 )
97
- # ts.loc[mask_shifted]
98
- # ts.loc[mask_shifted] = 2
102
+ with pytest .raises (IndexingError , match = msg ):
103
+ ts .loc [ mask_shifted ]
104
+ with pytest . raises ( IndexingError , match = msg ):
105
+ ts .loc [mask_shifted ] = 1
99
106
100
107
101
108
def test_setitem_boolean (test_data ):
@@ -168,14 +175,13 @@ def test_where_unsafe_upcast(dtype):
168
175
@pytest .mark .parametrize ("dtype" , [
169
176
np .int8 , np .int16 , np .int32 , np .float32
170
177
])
171
- def test_where_unsafe_itemsize_fail (dtype ):
172
- # Can't do these, as we are forced to change the
173
- # item size of the input to something we cannot.
178
+ def test_where_upcast (dtype ):
179
+ # see gh-9743
174
180
s = Series (np .arange (10 ), dtype = dtype )
175
181
mask = s < 5
176
182
177
183
values = [2.5 , 3.5 , 4.5 , 5.5 , 6.5 ]
178
- pytest . raises ( Exception , s . __setitem__ , tuple ( mask ), values )
184
+ s [ mask ] = values
179
185
180
186
181
187
def test_where_unsafe ():
@@ -206,10 +212,11 @@ def test_where_unsafe():
206
212
s = Series (np .arange (10 ))
207
213
mask = s > 5
208
214
209
- with pytest .raises (ValueError ):
215
+ msg = "cannot assign mismatch length to masked array"
216
+ with pytest .raises (ValueError , match = msg ):
210
217
s [mask ] = [5 , 4 , 3 , 2 , 1 ]
211
218
212
- with pytest .raises (ValueError ):
219
+ with pytest .raises (ValueError , match = msg ):
213
220
s [mask ] = [0 ] * 5
214
221
215
222
# dtype changes
@@ -276,8 +283,11 @@ def test_where_error():
276
283
s = Series (np .random .randn (5 ))
277
284
cond = s > 0
278
285
279
- pytest .raises (ValueError , s .where , 1 )
280
- pytest .raises (ValueError , s .where , cond [:3 ].values , - s )
286
+ msg = "Array conditional must be same shape as self"
287
+ with pytest .raises (ValueError , match = msg ):
288
+ s .where (1 )
289
+ with pytest .raises (ValueError , match = msg ):
290
+ s .where (cond [:3 ].values , - s )
281
291
282
292
# GH 2745
283
293
s = Series ([1 , 2 ])
@@ -286,10 +296,13 @@ def test_where_error():
286
296
assert_series_equal (s , expected )
287
297
288
298
# failures
289
- pytest .raises (ValueError , s .__setitem__ , tuple ([[[True , False ]]]),
290
- [0 , 2 , 3 ])
291
- pytest .raises (ValueError , s .__setitem__ , tuple ([[[True , False ]]]),
292
- [])
299
+ msg = "cannot assign mismatch length to masked array"
300
+ with pytest .raises (ValueError , match = msg ):
301
+ s [[True , False ]] = [0 , 2 , 3 ]
302
+ msg = ("NumPy boolean array indexing assignment cannot assign 0 input"
303
+ " values to the 1 output values where the mask is true" )
304
+ with pytest .raises (ValueError , match = msg ):
305
+ s [[True , False ]] = []
293
306
294
307
295
308
@pytest .mark .parametrize ('klass' , [list , tuple , np .array , Series ])
@@ -349,10 +362,13 @@ def test_where_setitem_invalid():
349
362
# GH 2702
350
363
# make sure correct exceptions are raised on invalid list assignment
351
364
365
+ msg = ("cannot set using a {} indexer with a different length than"
366
+ " the value" )
367
+
352
368
# slice
353
369
s = Series (list ('abc' ))
354
370
355
- with pytest .raises (ValueError ):
371
+ with pytest .raises (ValueError , match = msg . format ( 'slice' ) ):
356
372
s [0 :3 ] = list (range (27 ))
357
373
358
374
s [0 :3 ] = list (range (3 ))
@@ -362,7 +378,7 @@ def test_where_setitem_invalid():
362
378
# slice with step
363
379
s = Series (list ('abcdef' ))
364
380
365
- with pytest .raises (ValueError ):
381
+ with pytest .raises (ValueError , match = msg . format ( 'slice' ) ):
366
382
s [0 :4 :2 ] = list (range (27 ))
367
383
368
384
s = Series (list ('abcdef' ))
@@ -373,7 +389,7 @@ def test_where_setitem_invalid():
373
389
# neg slices
374
390
s = Series (list ('abcdef' ))
375
391
376
- with pytest .raises (ValueError ):
392
+ with pytest .raises (ValueError , match = msg . format ( 'slice' ) ):
377
393
s [:- 1 ] = list (range (27 ))
378
394
379
395
s [- 3 :- 1 ] = list (range (2 ))
@@ -383,12 +399,12 @@ def test_where_setitem_invalid():
383
399
# list
384
400
s = Series (list ('abc' ))
385
401
386
- with pytest .raises (ValueError ):
402
+ with pytest .raises (ValueError , match = msg . format ( 'list-like' ) ):
387
403
s [[0 , 1 , 2 ]] = list (range (27 ))
388
404
389
405
s = Series (list ('abc' ))
390
406
391
- with pytest .raises (ValueError ):
407
+ with pytest .raises (ValueError , match = msg . format ( 'list-like' ) ):
392
408
s [[0 , 1 , 2 ]] = list (range (2 ))
393
409
394
410
# scalar
@@ -590,8 +606,11 @@ def test_mask():
590
606
rs2 = s2 .mask (cond [:3 ], - s2 )
591
607
assert_series_equal (rs , rs2 )
592
608
593
- pytest .raises (ValueError , s .mask , 1 )
594
- pytest .raises (ValueError , s .mask , cond [:3 ].values , - s )
609
+ msg = "Array conditional must be same shape as self"
610
+ with pytest .raises (ValueError , match = msg ):
611
+ s .mask (1 )
612
+ with pytest .raises (ValueError , match = msg ):
613
+ s .mask (cond [:3 ].values , - s )
595
614
596
615
# dtype changes
597
616
s = Series ([1 , 2 , 3 , 4 ])
0 commit comments