1
- from warnings import catch_warnings
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 .util .testing as tm
8
6
from pandas .util .testing import assert_almost_equal , assert_series_equal
9
7
10
- ignore_ix = pytest .mark .filterwarnings ("ignore:\\ n.ix:FutureWarning" )
11
-
12
8
13
9
class TestFloatIndexers :
14
10
def check (self , result , original , indexer , getitem ):
@@ -62,7 +58,6 @@ def test_scalar_error(self):
62
58
with pytest .raises (TypeError , match = msg ):
63
59
s .iloc [3.0 ] = 0
64
60
65
- @ignore_ix
66
61
def test_scalar_non_numeric (self ):
67
62
68
63
# GH 4892
@@ -86,11 +81,7 @@ def test_scalar_non_numeric(self):
86
81
]:
87
82
88
83
# getting
89
- for idxr , getitem in [
90
- (lambda x : x .ix , False ),
91
- (lambda x : x .iloc , False ),
92
- (lambda x : x , True ),
93
- ]:
84
+ for idxr , getitem in [(lambda x : x .iloc , False ), (lambda x : x , True )]:
94
85
95
86
# gettitem on a DataFrame is a KeyError as it is indexing
96
87
# via labels on the columns
@@ -106,9 +97,8 @@ def test_scalar_non_numeric(self):
106
97
"Cannot index by location index with a"
107
98
" non-integer key" .format (klass = type (i ), kind = str (float ))
108
99
)
109
- with catch_warnings (record = True ):
110
- with pytest .raises (error , match = msg ):
111
- idxr (s )[3.0 ]
100
+ with pytest .raises (error , match = msg ):
101
+ idxr (s )[3.0 ]
112
102
113
103
# label based can be a TypeError or KeyError
114
104
if s .index .inferred_type in ["string" , "unicode" , "mixed" ]:
@@ -158,10 +148,9 @@ def test_scalar_non_numeric(self):
158
148
s2 .loc [3.0 ] = 10
159
149
assert s2 .index .is_object ()
160
150
161
- for idxr in [lambda x : x . ix , lambda x : x ]:
151
+ for idxr in [lambda x : x ]:
162
152
s2 = s .copy ()
163
- with catch_warnings (record = True ):
164
- idxr (s2 )[3.0 ] = 0
153
+ idxr (s2 )[3.0 ] = 0
165
154
assert s2 .index .is_object ()
166
155
167
156
# fallsback to position selection, series only
@@ -175,15 +164,14 @@ def test_scalar_non_numeric(self):
175
164
with pytest .raises (TypeError , match = msg ):
176
165
s [3.0 ]
177
166
178
- @ignore_ix
179
167
def test_scalar_with_mixed (self ):
180
168
181
169
s2 = Series ([1 , 2 , 3 ], index = ["a" , "b" , "c" ])
182
170
s3 = Series ([1 , 2 , 3 ], index = ["a" , "b" , 1.5 ])
183
171
184
172
# lookup in a pure stringstr
185
173
# with an invalid indexer
186
- for idxr in [lambda x : x . ix , lambda x : x , lambda x : x .iloc ]:
174
+ for idxr in [lambda x : x , lambda x : x .iloc ]:
187
175
188
176
msg = (
189
177
r"cannot do label indexing"
@@ -193,9 +181,8 @@ def test_scalar_with_mixed(self):
193
181
klass = str (Index ), kind = str (float )
194
182
)
195
183
)
196
- with catch_warnings (record = True ):
197
- with pytest .raises (TypeError , match = msg ):
198
- idxr (s2 )[1.0 ]
184
+ with pytest .raises (TypeError , match = msg ):
185
+ idxr (s2 )[1.0 ]
199
186
200
187
with pytest .raises (KeyError , match = r"^1$" ):
201
188
s2 .loc [1.0 ]
@@ -220,23 +207,6 @@ def test_scalar_with_mixed(self):
220
207
expected = 2
221
208
assert result == expected
222
209
223
- # mixed index so we have label
224
- # indexing
225
- for idxr in [lambda x : x .ix ]:
226
- with catch_warnings (record = True ):
227
-
228
- msg = (
229
- r"cannot do label indexing"
230
- r" on {klass} with these indexers \[1\.0\] of"
231
- r" {kind}" .format (klass = str (Index ), kind = str (float ))
232
- )
233
- with pytest .raises (TypeError , match = msg ):
234
- idxr (s3 )[1.0 ]
235
-
236
- result = idxr (s3 )[1 ]
237
- expected = 2
238
- assert result == expected
239
-
240
210
msg = "Cannot index by location index with a non-integer key"
241
211
with pytest .raises (TypeError , match = msg ):
242
212
s3 .iloc [1.0 ]
@@ -247,7 +217,6 @@ def test_scalar_with_mixed(self):
247
217
expected = 3
248
218
assert result == expected
249
219
250
- @ignore_ix
251
220
def test_scalar_integer (self ):
252
221
253
222
# test how scalar float indexers work on int indexes
@@ -261,22 +230,13 @@ def test_scalar_integer(self):
261
230
]:
262
231
263
232
# coerce to equal int
264
- for idxr , getitem in [
265
- (lambda x : x .ix , False ),
266
- (lambda x : x .loc , False ),
267
- (lambda x : x , True ),
268
- ]:
233
+ for idxr , getitem in [(lambda x : x .loc , False ), (lambda x : x , True )]:
269
234
270
- with catch_warnings (record = True ):
271
- result = idxr (s )[3.0 ]
235
+ result = idxr (s )[3.0 ]
272
236
self .check (result , s , 3 , getitem )
273
237
274
238
# coerce to equal int
275
- for idxr , getitem in [
276
- (lambda x : x .ix , False ),
277
- (lambda x : x .loc , False ),
278
- (lambda x : x , True ),
279
- ]:
239
+ for idxr , getitem in [(lambda x : x .loc , False ), (lambda x : x , True )]:
280
240
281
241
if isinstance (s , Series ):
282
242
@@ -292,20 +252,18 @@ def compare(x, y):
292
252
expected = Series (100.0 , index = range (len (s )), name = 3 )
293
253
294
254
s2 = s .copy ()
295
- with catch_warnings (record = True ):
296
- idxr (s2 )[3.0 ] = 100
255
+ idxr (s2 )[3.0 ] = 100
297
256
298
- result = idxr (s2 )[3.0 ]
299
- compare (result , expected )
257
+ result = idxr (s2 )[3.0 ]
258
+ compare (result , expected )
300
259
301
- result = idxr (s2 )[3 ]
302
- compare (result , expected )
260
+ result = idxr (s2 )[3 ]
261
+ compare (result , expected )
303
262
304
263
# contains
305
264
# coerce to equal int
306
265
assert 3.0 in s
307
266
308
- @ignore_ix
309
267
def test_scalar_float (self ):
310
268
311
269
# scalar float indexers work on a float index
@@ -319,11 +277,7 @@ def test_scalar_float(self):
319
277
320
278
# assert all operations except for iloc are ok
321
279
indexer = index [3 ]
322
- for idxr , getitem in [
323
- (lambda x : x .ix , False ),
324
- (lambda x : x .loc , False ),
325
- (lambda x : x , True ),
326
- ]:
280
+ for idxr , getitem in [(lambda x : x .loc , False ), (lambda x : x , True )]:
327
281
328
282
# getting
329
283
result = idxr (s )[indexer ]
@@ -332,14 +286,12 @@ def test_scalar_float(self):
332
286
# setting
333
287
s2 = s .copy ()
334
288
335
- with catch_warnings (record = True ):
336
- result = idxr (s2 )[indexer ]
289
+ result = idxr (s2 )[indexer ]
337
290
self .check (result , s , 3 , getitem )
338
291
339
292
# random integer is a KeyError
340
- with catch_warnings (record = True ):
341
- with pytest .raises (KeyError , match = r"^3\.5$" ):
342
- idxr (s )[3.5 ]
293
+ with pytest .raises (KeyError , match = r"^3\.5$" ):
294
+ idxr (s )[3.5 ]
343
295
344
296
# contains
345
297
assert 3.0 in s
@@ -365,7 +317,6 @@ def test_scalar_float(self):
365
317
with pytest .raises (TypeError , match = msg ):
366
318
s2 .iloc [3.0 ] = 0
367
319
368
- @ignore_ix
369
320
def test_slice_non_numeric (self ):
370
321
371
322
# GH 4892
@@ -397,12 +348,7 @@ def test_slice_non_numeric(self):
397
348
with pytest .raises (TypeError , match = msg ):
398
349
s .iloc [l ]
399
350
400
- for idxr in [
401
- lambda x : x .ix ,
402
- lambda x : x .loc ,
403
- lambda x : x .iloc ,
404
- lambda x : x ,
405
- ]:
351
+ for idxr in [lambda x : x .loc , lambda x : x .iloc , lambda x : x ]:
406
352
407
353
msg = (
408
354
"cannot do slice indexing"
@@ -414,9 +360,8 @@ def test_slice_non_numeric(self):
414
360
kind_int = str (int ),
415
361
)
416
362
)
417
- with catch_warnings (record = True ):
418
- with pytest .raises (TypeError , match = msg ):
419
- idxr (s )[l ]
363
+ with pytest .raises (TypeError , match = msg ):
364
+ idxr (s )[l ]
420
365
421
366
# setitem
422
367
for l in [slice (3.0 , 4 ), slice (3 , 4.0 ), slice (3.0 , 4.0 )]:
@@ -429,12 +374,7 @@ def test_slice_non_numeric(self):
429
374
with pytest .raises (TypeError , match = msg ):
430
375
s .iloc [l ] = 0
431
376
432
- for idxr in [
433
- lambda x : x .ix ,
434
- lambda x : x .loc ,
435
- lambda x : x .iloc ,
436
- lambda x : x ,
437
- ]:
377
+ for idxr in [lambda x : x .loc , lambda x : x .iloc , lambda x : x ]:
438
378
msg = (
439
379
"cannot do slice indexing"
440
380
r" on {klass} with these indexers"
@@ -445,11 +385,9 @@ def test_slice_non_numeric(self):
445
385
kind_int = str (int ),
446
386
)
447
387
)
448
- with catch_warnings (record = True ):
449
- with pytest .raises (TypeError , match = msg ):
450
- idxr (s )[l ] = 0
388
+ with pytest .raises (TypeError , match = msg ):
389
+ idxr (s )[l ] = 0
451
390
452
- @ignore_ix
453
391
def test_slice_integer (self ):
454
392
455
393
# same as above, but for Integer based indexes
@@ -468,10 +406,9 @@ def test_slice_integer(self):
468
406
# getitem
469
407
for l in [slice (3.0 , 4 ), slice (3 , 4.0 ), slice (3.0 , 4.0 )]:
470
408
471
- for idxr in [lambda x : x .loc , lambda x : x . ix ]:
409
+ for idxr in [lambda x : x .loc ]:
472
410
473
- with catch_warnings (record = True ):
474
- result = idxr (s )[l ]
411
+ result = idxr (s )[l ]
475
412
476
413
# these are all label indexing
477
414
# except getitem which is positional
@@ -494,9 +431,8 @@ def test_slice_integer(self):
494
431
# getitem out-of-bounds
495
432
for l in [slice (- 6 , 6 ), slice (- 6.0 , 6.0 )]:
496
433
497
- for idxr in [lambda x : x .loc , lambda x : x .ix ]:
498
- with catch_warnings (record = True ):
499
- result = idxr (s )[l ]
434
+ for idxr in [lambda x : x .loc ]:
435
+ result = idxr (s )[l ]
500
436
501
437
# these are all label indexing
502
438
# except getitem which is positional
@@ -523,10 +459,9 @@ def test_slice_integer(self):
523
459
(slice (2.5 , 3.5 ), slice (3 , 4 )),
524
460
]:
525
461
526
- for idxr in [lambda x : x .loc , lambda x : x . ix ]:
462
+ for idxr in [lambda x : x .loc ]:
527
463
528
- with catch_warnings (record = True ):
529
- result = idxr (s )[l ]
464
+ result = idxr (s )[l ]
530
465
if oob :
531
466
res = slice (0 , 0 )
532
467
else :
@@ -546,11 +481,10 @@ def test_slice_integer(self):
546
481
# setitem
547
482
for l in [slice (3.0 , 4 ), slice (3 , 4.0 ), slice (3.0 , 4.0 )]:
548
483
549
- for idxr in [lambda x : x .loc , lambda x : x . ix ]:
484
+ for idxr in [lambda x : x .loc ]:
550
485
sc = s .copy ()
551
- with catch_warnings (record = True ):
552
- idxr (sc )[l ] = 0
553
- result = idxr (sc )[l ].values .ravel ()
486
+ idxr (sc )[l ] = 0
487
+ result = idxr (sc )[l ].values .ravel ()
554
488
assert (result == 0 ).all ()
555
489
556
490
# positional indexing
@@ -585,7 +519,6 @@ def test_integer_positional_indexing(self):
585
519
with pytest .raises (TypeError , match = msg ):
586
520
idxr (s )[l ]
587
521
588
- @ignore_ix
589
522
def test_slice_integer_frame_getitem (self ):
590
523
591
524
# similar to above, but on the getitem dim (of a DataFrame)
@@ -663,10 +596,7 @@ def f(idxr):
663
596
s [l ] = 0
664
597
665
598
f (lambda x : x .loc )
666
- with catch_warnings (record = True ):
667
- f (lambda x : x .ix )
668
599
669
- @ignore_ix
670
600
def test_slice_float (self ):
671
601
672
602
# same as above, but for floats
@@ -679,20 +609,18 @@ def test_slice_float(self):
679
609
for l in [slice (3.0 , 4 ), slice (3 , 4.0 ), slice (3.0 , 4.0 )]:
680
610
681
611
expected = s .iloc [3 :4 ]
682
- for idxr in [lambda x : x .ix , lambda x : x . loc , lambda x : x ]:
612
+ for idxr in [lambda x : x .loc , lambda x : x ]:
683
613
684
614
# getitem
685
- with catch_warnings (record = True ):
686
- result = idxr (s )[l ]
615
+ result = idxr (s )[l ]
687
616
if isinstance (s , Series ):
688
617
tm .assert_series_equal (result , expected )
689
618
else :
690
619
tm .assert_frame_equal (result , expected )
691
620
# setitem
692
621
s2 = s .copy ()
693
- with catch_warnings (record = True ):
694
- idxr (s2 )[l ] = 0
695
- result = idxr (s2 )[l ].values .ravel ()
622
+ idxr (s2 )[l ] = 0
623
+ result = idxr (s2 )[l ].values .ravel ()
696
624
assert (result == 0 ).all ()
697
625
698
626
def test_floating_index_doc_example (self ):
0 commit comments