@@ -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 ):
@@ -165,6 +172,7 @@ def test_where_unsafe_upcast(dtype):
165
172
assert_series_equal (s , expected )
166
173
167
174
175
+ @pytest .mark .xfail
168
176
@pytest .mark .parametrize ("dtype" , [
169
177
np .int8 , np .int16 , np .int32 , np .float32
170
178
])
@@ -175,7 +183,8 @@ def test_where_unsafe_itemsize_fail(dtype):
175
183
mask = s < 5
176
184
177
185
values = [2.5 , 3.5 , 4.5 , 5.5 , 6.5 ]
178
- pytest .raises (Exception , s .__setitem__ , tuple (mask ), values )
186
+ with pytest .raises (Exception ):
187
+ s [mask ] = values
179
188
180
189
181
190
def test_where_unsafe ():
@@ -206,10 +215,11 @@ def test_where_unsafe():
206
215
s = Series (np .arange (10 ))
207
216
mask = s > 5
208
217
209
- with pytest .raises (ValueError ):
218
+ msg = "cannot assign mismatch length to masked array"
219
+ with pytest .raises (ValueError , match = msg ):
210
220
s [mask ] = [5 , 4 , 3 , 2 , 1 ]
211
221
212
- with pytest .raises (ValueError ):
222
+ with pytest .raises (ValueError , match = msg ):
213
223
s [mask ] = [0 ] * 5
214
224
215
225
# dtype changes
@@ -276,8 +286,11 @@ def test_where_error():
276
286
s = Series (np .random .randn (5 ))
277
287
cond = s > 0
278
288
279
- pytest .raises (ValueError , s .where , 1 )
280
- pytest .raises (ValueError , s .where , cond [:3 ].values , - s )
289
+ msg = "Array conditional must be same shape as self"
290
+ with pytest .raises (ValueError , match = msg ):
291
+ s .where (1 )
292
+ with pytest .raises (ValueError , match = msg ):
293
+ s .where (cond [:3 ].values , - s )
281
294
282
295
# GH 2745
283
296
s = Series ([1 , 2 ])
@@ -286,10 +299,13 @@ def test_where_error():
286
299
assert_series_equal (s , expected )
287
300
288
301
# failures
289
- pytest .raises (ValueError , s .__setitem__ , tuple ([[[True , False ]]]),
290
- [0 , 2 , 3 ])
291
- pytest .raises (ValueError , s .__setitem__ , tuple ([[[True , False ]]]),
292
- [])
302
+ msg = "cannot assign mismatch length to masked array"
303
+ with pytest .raises (ValueError , match = msg ):
304
+ s [[True , False ]] = [0 , 2 , 3 ]
305
+ msg = ("NumPy boolean array indexing assignment cannot assign 0 input"
306
+ " values to the 1 output values where the mask is true" )
307
+ with pytest .raises (ValueError , match = msg ):
308
+ s [[True , False ]] = []
293
309
294
310
295
311
@pytest .mark .parametrize ('klass' , [list , tuple , np .array , Series ])
@@ -349,10 +365,13 @@ def test_where_setitem_invalid():
349
365
# GH 2702
350
366
# make sure correct exceptions are raised on invalid list assignment
351
367
368
+ msg = ("cannot set using a {} indexer with a different length than"
369
+ " the value" )
370
+
352
371
# slice
353
372
s = Series (list ('abc' ))
354
373
355
- with pytest .raises (ValueError ):
374
+ with pytest .raises (ValueError , match = msg . format ( 'slice' ) ):
356
375
s [0 :3 ] = list (range (27 ))
357
376
358
377
s [0 :3 ] = list (range (3 ))
@@ -362,7 +381,7 @@ def test_where_setitem_invalid():
362
381
# slice with step
363
382
s = Series (list ('abcdef' ))
364
383
365
- with pytest .raises (ValueError ):
384
+ with pytest .raises (ValueError , match = msg . format ( 'slice' ) ):
366
385
s [0 :4 :2 ] = list (range (27 ))
367
386
368
387
s = Series (list ('abcdef' ))
@@ -373,7 +392,7 @@ def test_where_setitem_invalid():
373
392
# neg slices
374
393
s = Series (list ('abcdef' ))
375
394
376
- with pytest .raises (ValueError ):
395
+ with pytest .raises (ValueError , match = msg . format ( 'slice' ) ):
377
396
s [:- 1 ] = list (range (27 ))
378
397
379
398
s [- 3 :- 1 ] = list (range (2 ))
@@ -383,12 +402,12 @@ def test_where_setitem_invalid():
383
402
# list
384
403
s = Series (list ('abc' ))
385
404
386
- with pytest .raises (ValueError ):
405
+ with pytest .raises (ValueError , match = msg . format ( 'list-like' ) ):
387
406
s [[0 , 1 , 2 ]] = list (range (27 ))
388
407
389
408
s = Series (list ('abc' ))
390
409
391
- with pytest .raises (ValueError ):
410
+ with pytest .raises (ValueError , match = msg . format ( 'list-like' ) ):
392
411
s [[0 , 1 , 2 ]] = list (range (2 ))
393
412
394
413
# scalar
@@ -590,8 +609,11 @@ def test_mask():
590
609
rs2 = s2 .mask (cond [:3 ], - s2 )
591
610
assert_series_equal (rs , rs2 )
592
611
593
- pytest .raises (ValueError , s .mask , 1 )
594
- pytest .raises (ValueError , s .mask , cond [:3 ].values , - s )
612
+ msg = "Array conditional must be same shape as self"
613
+ with pytest .raises (ValueError , match = msg ):
614
+ s .mask (1 )
615
+ with pytest .raises (ValueError , match = msg ):
616
+ s .mask (cond [:3 ].values , - s )
595
617
596
618
# dtype changes
597
619
s = Series ([1 , 2 , 3 , 4 ])
0 commit comments