1
+ import re
2
+
1
3
import numpy as np
2
4
import pytest
3
5
4
6
from pandas import DataFrame , Float64Index , Index , Int64Index , RangeIndex , Series
5
7
import pandas ._testing as tm
6
8
9
+ # We pass through the error message from numpy
10
+ _slice_iloc_msg = re .escape (
11
+ "only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) "
12
+ "and integer or boolean arrays are valid indices"
13
+ )
14
+
7
15
8
16
def gen_obj (klass , index ):
9
17
if klass is Series :
@@ -62,11 +70,7 @@ def test_scalar_error(self, index_func):
62
70
with pytest .raises (TypeError , match = msg ):
63
71
s .iloc [3.0 ]
64
72
65
- msg = (
66
- f"cannot do positional indexing on { type (i ).__name__ } with these "
67
- r"indexers \[3\.0\] of type float"
68
- )
69
- with pytest .raises (TypeError , match = msg ):
73
+ with pytest .raises (IndexError , match = _slice_iloc_msg ):
70
74
s .iloc [3.0 ] = 0
71
75
72
76
@pytest .mark .parametrize (
@@ -133,12 +137,7 @@ def test_scalar_non_numeric(self, index_func, klass):
133
137
assert 3.0 not in s
134
138
135
139
# setting with a float fails with iloc
136
- msg = (
137
- r"cannot do (label|positional) indexing "
138
- fr"on { type (i ).__name__ } with these indexers \[3\.0\] of "
139
- "type float"
140
- )
141
- with pytest .raises (TypeError , match = msg ):
140
+ with pytest .raises (IndexError , match = _slice_iloc_msg ):
142
141
s .iloc [3.0 ] = 0
143
142
144
143
# setting with an indexer
@@ -327,12 +326,7 @@ def test_scalar_float(self, klass):
327
326
with pytest .raises (TypeError , match = msg ):
328
327
s .iloc [3.0 ]
329
328
330
- msg = (
331
- "cannot do positional indexing "
332
- fr"on { Float64Index .__name__ } with these indexers \[3\.0\] of "
333
- "type float"
334
- )
335
- with pytest .raises (TypeError , match = msg ):
329
+ with pytest .raises (IndexError , match = _slice_iloc_msg ):
336
330
s2 .iloc [3.0 ] = 0
337
331
338
332
@pytest .mark .parametrize (
@@ -376,11 +370,7 @@ def test_slice_non_numeric(self, index_func, l, klass):
376
370
idxr (s )[l ]
377
371
378
372
# setitem
379
- msg = (
380
- "cannot do positional indexing "
381
- fr"on { type (index ).__name__ } with these indexers \[(3|4)\.0\] of "
382
- "type float"
383
- )
373
+ msg = "slice indices must be integers or None or have an __index__ method"
384
374
with pytest .raises (TypeError , match = msg ):
385
375
s .iloc [l ] = 0
386
376
@@ -390,7 +380,7 @@ def test_slice_non_numeric(self, index_func, l, klass):
390
380
r"\[(3|4)(\.0)?\] "
391
381
r"of type (float|int)"
392
382
)
393
- for idxr in [lambda x : x .loc , lambda x : x . iloc , lambda x : x ]:
383
+ for idxr in [lambda x : x .loc , lambda x : x ]:
394
384
with pytest .raises (TypeError , match = msg ):
395
385
idxr (s )[l ] = 0
396
386
0 commit comments