@@ -50,7 +50,7 @@ def check(self, result, original, indexer, getitem):
50
50
tm .makePeriodIndex ,
51
51
],
52
52
)
53
- def test_scalar_non_numeric (self , index_func , frame_or_series ):
53
+ def test_scalar_non_numeric (self , index_func , frame_or_series , indexer_sl ):
54
54
55
55
# GH 4892
56
56
# float_indexers should raise exceptions
@@ -61,10 +61,7 @@ def test_scalar_non_numeric(self, index_func, frame_or_series):
61
61
62
62
# getting
63
63
with pytest .raises (KeyError , match = "^3.0$" ):
64
- s [3.0 ]
65
-
66
- with pytest .raises (KeyError , match = "^3.0$" ):
67
- s .loc [3.0 ]
64
+ indexer_sl (s )[3.0 ]
68
65
69
66
# contains
70
67
assert 3.0 not in s
@@ -88,11 +85,7 @@ def test_scalar_non_numeric(self, index_func, frame_or_series):
88
85
else :
89
86
90
87
s2 = s .copy ()
91
- s2 .loc [3.0 ] = 10
92
- assert s2 .index .is_object ()
93
-
94
- s2 = s .copy ()
95
- s2 [3.0 ] = 0
88
+ indexer_sl (s2 )[3.0 ] = 10
96
89
assert s2 .index .is_object ()
97
90
98
91
@pytest .mark .parametrize (
@@ -114,44 +107,44 @@ def test_scalar_non_numeric_series_fallback(self, index_func):
114
107
with pytest .raises (KeyError , match = "^3.0$" ):
115
108
s [3.0 ]
116
109
117
- def test_scalar_with_mixed (self ):
110
+ def test_scalar_with_mixed (self , indexer_sl ):
118
111
119
112
s2 = Series ([1 , 2 , 3 ], index = ["a" , "b" , "c" ])
120
113
s3 = Series ([1 , 2 , 3 ], index = ["a" , "b" , 1.5 ])
121
114
122
115
# lookup in a pure string index with an invalid indexer
123
116
124
117
with pytest .raises (KeyError , match = "^1.0$" ):
125
- s2 [1.0 ]
118
+ indexer_sl ( s2 ) [1.0 ]
126
119
127
120
with pytest .raises (KeyError , match = r"^1\.0$" ):
128
- s2 . loc [1.0 ]
121
+ indexer_sl ( s2 ) [1.0 ]
129
122
130
- result = s2 . loc ["b" ]
123
+ result = indexer_sl ( s2 ) ["b" ]
131
124
expected = 2
132
125
assert result == expected
133
126
134
127
# mixed index so we have label
135
128
# indexing
136
129
with pytest .raises (KeyError , match = "^1.0$" ):
137
- s3 [1.0 ]
130
+ indexer_sl ( s3 ) [1.0 ]
138
131
139
- result = s3 [1 ]
140
- expected = 2
141
- assert result == expected
132
+ if indexer_sl is not tm .loc :
133
+ # __getitem__ falls back to positional
134
+ result = s3 [1 ]
135
+ expected = 2
136
+ assert result == expected
142
137
143
138
with pytest .raises (KeyError , match = r"^1\.0$" ):
144
- s3 . loc [1.0 ]
139
+ indexer_sl ( s3 ) [1.0 ]
145
140
146
- result = s3 . loc [1.5 ]
141
+ result = indexer_sl ( s3 ) [1.5 ]
147
142
expected = 3
148
143
assert result == expected
149
144
150
- @pytest .mark .parametrize (
151
- "idxr,getitem" , [(lambda x : x .loc , False ), (lambda x : x , True )]
152
- )
153
145
@pytest .mark .parametrize ("index_func" , [tm .makeIntIndex , tm .makeRangeIndex ])
154
- def test_scalar_integer (self , index_func , frame_or_series , idxr , getitem ):
146
+ def test_scalar_integer (self , index_func , frame_or_series , indexer_sl ):
147
+ getitem = indexer_sl is not tm .loc
155
148
156
149
# test how scalar float indexers work on int indexes
157
150
@@ -161,7 +154,7 @@ def test_scalar_integer(self, index_func, frame_or_series, idxr, getitem):
161
154
162
155
# coerce to equal int
163
156
164
- result = idxr (obj )[3.0 ]
157
+ result = indexer_sl (obj )[3.0 ]
165
158
self .check (result , obj , 3 , getitem )
166
159
167
160
if isinstance (obj , Series ):
@@ -178,12 +171,12 @@ def compare(x, y):
178
171
expected = Series (100.0 , index = range (len (obj )), name = 3 )
179
172
180
173
s2 = obj .copy ()
181
- idxr (s2 )[3.0 ] = 100
174
+ indexer_sl (s2 )[3.0 ] = 100
182
175
183
- result = idxr (s2 )[3.0 ]
176
+ result = indexer_sl (s2 )[3.0 ]
184
177
compare (result , expected )
185
178
186
- result = idxr (s2 )[3 ]
179
+ result = indexer_sl (s2 )[3 ]
187
180
compare (result , expected )
188
181
189
182
@pytest .mark .parametrize ("index_func" , [tm .makeIntIndex , tm .makeRangeIndex ])
@@ -204,7 +197,8 @@ def test_scalar_float(self, frame_or_series):
204
197
205
198
# assert all operations except for iloc are ok
206
199
indexer = index [3 ]
207
- for idxr , getitem in [(lambda x : x .loc , False ), (lambda x : x , True )]:
200
+ for idxr in [tm .loc , tm .setitem ]:
201
+ getitem = idxr is not tm .loc
208
202
209
203
# getting
210
204
result = idxr (s )[indexer ]
@@ -242,7 +236,7 @@ def test_scalar_float(self, frame_or_series):
242
236
],
243
237
)
244
238
@pytest .mark .parametrize ("idx" , [slice (3.0 , 4 ), slice (3 , 4.0 ), slice (3.0 , 4.0 )])
245
- def test_slice_non_numeric (self , index_func , idx , frame_or_series ):
239
+ def test_slice_non_numeric (self , index_func , idx , frame_or_series , indexer_sli ):
246
240
247
241
# GH 4892
248
242
# float_indexers should raise exceptions
@@ -252,38 +246,28 @@ def test_slice_non_numeric(self, index_func, idx, frame_or_series):
252
246
s = gen_obj (frame_or_series , index )
253
247
254
248
# getitem
255
- msg = (
256
- "cannot do positional indexing "
257
- fr"on { type (index ).__name__ } with these indexers \[(3|4)\.0\] of "
258
- "type float"
259
- )
249
+ if indexer_sli is tm .iloc :
250
+ msg = (
251
+ "cannot do positional indexing "
252
+ fr"on { type (index ).__name__ } with these indexers \[(3|4)\.0\] of "
253
+ "type float"
254
+ )
255
+ else :
256
+ msg = (
257
+ "cannot do slice indexing "
258
+ fr"on { type (index ).__name__ } with these indexers "
259
+ r"\[(3|4)(\.0)?\] "
260
+ r"of type (float|int)"
261
+ )
260
262
with pytest .raises (TypeError , match = msg ):
261
- s .iloc [idx ]
262
-
263
- msg = (
264
- "cannot do (slice|positional) indexing "
265
- fr"on { type (index ).__name__ } with these indexers "
266
- r"\[(3|4)(\.0)?\] "
267
- r"of type (float|int)"
268
- )
269
- for idxr in [lambda x : x .loc , lambda x : x .iloc , lambda x : x ]:
270
- with pytest .raises (TypeError , match = msg ):
271
- idxr (s )[idx ]
263
+ indexer_sli (s )[idx ]
272
264
273
265
# setitem
274
- msg = "slice indices must be integers or None or have an __index__ method"
266
+ if indexer_sli is tm .iloc :
267
+ # otherwise we keep the same message as above
268
+ msg = "slice indices must be integers or None or have an __index__ method"
275
269
with pytest .raises (TypeError , match = msg ):
276
- s .iloc [idx ] = 0
277
-
278
- msg = (
279
- "cannot do (slice|positional) indexing "
280
- fr"on { type (index ).__name__ } with these indexers "
281
- r"\[(3|4)(\.0)?\] "
282
- r"of type (float|int)"
283
- )
284
- for idxr in [lambda x : x .loc , lambda x : x ]:
285
- with pytest .raises (TypeError , match = msg ):
286
- idxr (s )[idx ] = 0
270
+ indexer_sli (s )[idx ] = 0
287
271
288
272
def test_slice_integer (self ):
289
273
@@ -469,25 +453,24 @@ def test_float_slice_getitem_with_integer_index_raises(self, idx, index_func):
469
453
s [idx ]
470
454
471
455
@pytest .mark .parametrize ("idx" , [slice (3.0 , 4 ), slice (3 , 4.0 ), slice (3.0 , 4.0 )])
472
- def test_slice_float (self , idx , frame_or_series ):
456
+ def test_slice_float (self , idx , frame_or_series , indexer_sl ):
473
457
474
458
# same as above, but for floats
475
459
index = Index (np .arange (5.0 )) + 0.1
476
460
s = gen_obj (frame_or_series , index )
477
461
478
462
expected = s .iloc [3 :4 ]
479
- for idxr in [lambda x : x .loc , lambda x : x ]:
480
463
481
- # getitem
482
- result = idxr (s )[idx ]
483
- assert isinstance (result , type (s ))
484
- tm .assert_equal (result , expected )
464
+ # getitem
465
+ result = indexer_sl (s )[idx ]
466
+ assert isinstance (result , type (s ))
467
+ tm .assert_equal (result , expected )
485
468
486
- # setitem
487
- s2 = s .copy ()
488
- idxr (s2 )[idx ] = 0
489
- result = idxr (s2 )[idx ].values .ravel ()
490
- assert (result == 0 ).all ()
469
+ # setitem
470
+ s2 = s .copy ()
471
+ indexer_sl (s2 )[idx ] = 0
472
+ result = indexer_sl (s2 )[idx ].values .ravel ()
473
+ assert (result == 0 ).all ()
491
474
492
475
def test_floating_index_doc_example (self ):
493
476
@@ -564,19 +547,6 @@ def test_floating_misc(self, indexer_sl):
564
547
result = indexer_sl (s )[[2.5 ]]
565
548
tm .assert_series_equal (result , Series ([1 ], index = [2.5 ]))
566
549
567
- def test_floating_tuples (self ):
568
- # see gh-13509
569
- s = Series ([(1 , 1 ), (2 , 2 ), (3 , 3 )], index = [0.0 , 0.1 , 0.2 ], name = "foo" )
570
-
571
- result = s [0.0 ]
572
- assert result == (1 , 1 )
573
-
574
- expected = Series ([(1 , 1 ), (2 , 2 )], index = [0.0 , 0.0 ], name = "foo" )
575
- s = Series ([(1 , 1 ), (2 , 2 ), (3 , 3 )], index = [0.0 , 0.0 , 0.2 ], name = "foo" )
576
-
577
- result = s [0.0 ]
578
- tm .assert_series_equal (result , expected )
579
-
580
550
def test_float64index_slicing_bug (self ):
581
551
# GH 5557, related to slicing a float index
582
552
ser = {
0 commit comments