1
- import re
2
-
3
1
import numpy as np
4
2
import pytest
5
3
6
4
from pandas import DataFrame , Float64Index , Index , Int64Index , RangeIndex , Series
7
5
import pandas ._testing as tm
8
6
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
-
15
7
16
8
def gen_obj (klass , index ):
17
9
if klass is Series :
@@ -40,24 +32,6 @@ def check(self, result, original, indexer, getitem):
40
32
41
33
tm .assert_almost_equal (result , expected )
42
34
43
- def test_scalar_error (self , series_with_simple_index ):
44
-
45
- # GH 4892
46
- # float_indexers should raise exceptions
47
- # on appropriate Index types & accessors
48
- # this duplicates the code below
49
- # but is specifically testing for the error
50
- # message
51
-
52
- s = series_with_simple_index
53
-
54
- msg = "Cannot index by location index with a non-integer key"
55
- with pytest .raises (TypeError , match = msg ):
56
- s .iloc [3.0 ]
57
-
58
- with pytest .raises (IndexError , match = _slice_iloc_msg ):
59
- s .iloc [3.0 ] = 0
60
-
61
35
@pytest .mark .parametrize (
62
36
"index_func" ,
63
37
[
@@ -69,40 +43,32 @@ def test_scalar_error(self, series_with_simple_index):
69
43
tm .makePeriodIndex ,
70
44
],
71
45
)
72
- @pytest .mark .parametrize ("klass" , [Series , DataFrame ])
73
- def test_scalar_non_numeric (self , index_func , klass ):
46
+ def test_scalar_non_numeric (self , index_func , frame_or_series ):
74
47
75
48
# GH 4892
76
49
# float_indexers should raise exceptions
77
50
# on appropriate Index types & accessors
78
51
79
52
i = index_func (5 )
80
- s = gen_obj (klass , i )
53
+ s = gen_obj (frame_or_series , i )
81
54
82
55
# getting
83
56
with pytest .raises (KeyError , match = "^3.0$" ):
84
57
s [3.0 ]
85
58
86
- msg = "Cannot index by location index with a non-integer key"
87
- with pytest .raises (TypeError , match = msg ):
88
- s .iloc [3.0 ]
89
-
90
59
with pytest .raises (KeyError , match = "^3.0$" ):
91
60
s .loc [3.0 ]
92
61
93
62
# contains
94
63
assert 3.0 not in s
95
64
96
- # setting with a float fails with iloc
97
- with pytest .raises (IndexError , match = _slice_iloc_msg ):
98
- s .iloc [3.0 ] = 0
99
-
100
65
# setting with an indexer
101
66
if s .index .inferred_type in ["categorical" ]:
102
67
# Value or Type Error
103
68
pass
104
69
elif s .index .inferred_type in ["datetime64" , "timedelta64" , "period" ]:
105
70
71
+ # FIXME: dont leave commented-out
106
72
# these should prob work
107
73
# and are inconsistent between series/dataframe ATM
108
74
# for idxr in [lambda x: x]:
@@ -151,10 +117,6 @@ def test_scalar_with_mixed(self):
151
117
with pytest .raises (KeyError , match = "^1.0$" ):
152
118
s2 [1.0 ]
153
119
154
- msg = "Cannot index by location index with a non-integer key"
155
- with pytest .raises (TypeError , match = msg ):
156
- s2 .iloc [1.0 ]
157
-
158
120
with pytest .raises (KeyError , match = r"^1\.0$" ):
159
121
s2 .loc [1.0 ]
160
122
@@ -171,9 +133,6 @@ def test_scalar_with_mixed(self):
171
133
expected = 2
172
134
assert result == expected
173
135
174
- msg = "Cannot index by location index with a non-integer key"
175
- with pytest .raises (TypeError , match = msg ):
176
- s3 .iloc [1.0 ]
177
136
with pytest .raises (KeyError , match = r"^1\.0$" ):
178
137
s3 .loc [1.0 ]
179
138
@@ -182,14 +141,13 @@ def test_scalar_with_mixed(self):
182
141
assert result == expected
183
142
184
143
@pytest .mark .parametrize ("index_func" , [tm .makeIntIndex , tm .makeRangeIndex ])
185
- @pytest .mark .parametrize ("klass" , [Series , DataFrame ])
186
- def test_scalar_integer (self , index_func , klass ):
144
+ def test_scalar_integer (self , index_func , frame_or_series ):
187
145
188
146
# test how scalar float indexers work on int indexes
189
147
190
148
# integer index
191
149
i = index_func (5 )
192
- obj = gen_obj (klass , i )
150
+ obj = gen_obj (frame_or_series , i )
193
151
194
152
# coerce to equal int
195
153
for idxr , getitem in [(lambda x : x .loc , False ), (lambda x : x , True )]:
@@ -226,12 +184,11 @@ def compare(x, y):
226
184
# coerce to equal int
227
185
assert 3.0 in obj
228
186
229
- @pytest .mark .parametrize ("klass" , [Series , DataFrame ])
230
- def test_scalar_float (self , klass ):
187
+ def test_scalar_float (self , frame_or_series ):
231
188
232
189
# scalar float indexers work on a float index
233
190
index = Index (np .arange (5.0 ))
234
- s = gen_obj (klass , index )
191
+ s = gen_obj (frame_or_series , index )
235
192
236
193
# assert all operations except for iloc are ok
237
194
indexer = index [3 ]
@@ -262,14 +219,6 @@ def test_scalar_float(self, klass):
262
219
result = s2 .iloc [3 ]
263
220
self .check (result , s , 3 , False )
264
221
265
- # iloc raises with a float
266
- msg = "Cannot index by location index with a non-integer key"
267
- with pytest .raises (TypeError , match = msg ):
268
- s .iloc [3.0 ]
269
-
270
- with pytest .raises (IndexError , match = _slice_iloc_msg ):
271
- s2 .iloc [3.0 ] = 0
272
-
273
222
@pytest .mark .parametrize (
274
223
"index_func" ,
275
224
[
@@ -281,15 +230,14 @@ def test_scalar_float(self, klass):
281
230
],
282
231
)
283
232
@pytest .mark .parametrize ("l" , [slice (3.0 , 4 ), slice (3 , 4.0 ), slice (3.0 , 4.0 )])
284
- @pytest .mark .parametrize ("klass" , [Series , DataFrame ])
285
- def test_slice_non_numeric (self , index_func , l , klass ):
233
+ def test_slice_non_numeric (self , index_func , l , frame_or_series ):
286
234
287
235
# GH 4892
288
236
# float_indexers should raise exceptions
289
237
# on appropriate Index types & accessors
290
238
291
239
index = index_func (5 )
292
- s = gen_obj (klass , index )
240
+ s = gen_obj (frame_or_series , index )
293
241
294
242
# getitem
295
243
msg = (
@@ -509,12 +457,11 @@ def test_float_slice_getitem_with_integer_index_raises(self, l, index_func):
509
457
s [l ]
510
458
511
459
@pytest .mark .parametrize ("l" , [slice (3.0 , 4 ), slice (3 , 4.0 ), slice (3.0 , 4.0 )])
512
- @pytest .mark .parametrize ("klass" , [Series , DataFrame ])
513
- def test_slice_float (self , l , klass ):
460
+ def test_slice_float (self , l , frame_or_series ):
514
461
515
462
# same as above, but for floats
516
463
index = Index (np .arange (5.0 )) + 0.1
517
- s = gen_obj (klass , index )
464
+ s = gen_obj (frame_or_series , index )
518
465
519
466
expected = s .iloc [3 :4 ]
520
467
for idxr in [lambda x : x .loc , lambda x : x ]:
0 commit comments