1+ import re
2+
13import numpy as np
24import pytest
35
46from pandas import DataFrame , Float64Index , Index , Int64Index , RangeIndex , Series
57import pandas ._testing as tm
68
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+
715
816def gen_obj (klass , index ):
917 if klass is Series :
@@ -62,11 +70,7 @@ def test_scalar_error(self, index_func):
6270 with pytest .raises (TypeError , match = msg ):
6371 s .iloc [3.0 ]
6472
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 ):
7074 s .iloc [3.0 ] = 0
7175
7276 @pytest .mark .parametrize (
@@ -133,12 +137,7 @@ def test_scalar_non_numeric(self, index_func, klass):
133137 assert 3.0 not in s
134138
135139 # 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 ):
142141 s .iloc [3.0 ] = 0
143142
144143 # setting with an indexer
@@ -327,12 +326,7 @@ def test_scalar_float(self, klass):
327326 with pytest .raises (TypeError , match = msg ):
328327 s .iloc [3.0 ]
329328
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 ):
336330 s2 .iloc [3.0 ] = 0
337331
338332 @pytest .mark .parametrize (
@@ -376,11 +370,7 @@ def test_slice_non_numeric(self, index_func, l, klass):
376370 idxr (s )[l ]
377371
378372 # 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"
384374 with pytest .raises (TypeError , match = msg ):
385375 s .iloc [l ] = 0
386376
@@ -390,7 +380,7 @@ def test_slice_non_numeric(self, index_func, l, klass):
390380 r"\[(3|4)(\.0)?\] "
391381 r"of type (float|int)"
392382 )
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 ]:
394384 with pytest .raises (TypeError , match = msg ):
395385 idxr (s )[l ] = 0
396386
0 commit comments