10
10
Timestamp ,
11
11
)
12
12
import pandas ._testing as tm
13
- from pandas .core .indexes .api import (
14
- Float64Index ,
15
- Int64Index ,
16
- UInt64Index ,
17
- )
18
13
19
14
20
15
@pytest .fixture
21
16
def index_large ():
22
- # large values used in UInt64Index tests where no compat needed with Int64/Float64
17
+ # large values used in Index[uint64] tests where no compat needed with Int64/Float64
23
18
large = [2 ** 63 , 2 ** 63 + 10 , 2 ** 63 + 15 , 2 ** 63 + 20 , 2 ** 63 + 25 ]
24
- return UInt64Index (large )
19
+ return Index (large , dtype = np . uint64 )
25
20
26
21
27
22
class TestGetLoc :
@@ -86,7 +81,7 @@ def test_get_loc_raises_missized_tolerance(self):
86
81
87
82
@pytest .mark .filterwarnings ("ignore:Passing method:FutureWarning" )
88
83
def test_get_loc_float64 (self ):
89
- idx = Float64Index ([0.0 , 1.0 , 2.0 ])
84
+ idx = Index ([0.0 , 1.0 , 2.0 ], dtype = np . float64 )
90
85
for method in [None , "pad" , "backfill" , "nearest" ]:
91
86
assert idx .get_loc (1 , method ) == 1
92
87
if method is not None :
@@ -119,27 +114,27 @@ def test_get_loc_float64(self):
119
114
idx .get_loc (1.4 , method = "nearest" , tolerance = np .array ([1 , 2 ]))
120
115
121
116
def test_get_loc_na (self ):
122
- idx = Float64Index ([np .nan , 1 , 2 ])
117
+ idx = Index ([np .nan , 1 , 2 ], dtype = np . float64 )
123
118
assert idx .get_loc (1 ) == 1
124
119
assert idx .get_loc (np .nan ) == 0
125
120
126
- idx = Float64Index ([np .nan , 1 , np .nan ])
121
+ idx = Index ([np .nan , 1 , np .nan ], dtype = np . float64 )
127
122
assert idx .get_loc (1 ) == 1
128
123
129
124
# representable by slice [0:2:2]
130
125
msg = "'Cannot get left slice bound for non-unique label: nan'"
131
126
with pytest .raises (KeyError , match = msg ):
132
127
idx .slice_locs (np .nan )
133
128
# not representable by slice
134
- idx = Float64Index ([np .nan , 1 , np .nan , np .nan ])
129
+ idx = Index ([np .nan , 1 , np .nan , np .nan ], dtype = np . float64 )
135
130
assert idx .get_loc (1 ) == 1
136
131
msg = "'Cannot get left slice bound for non-unique label: nan"
137
132
with pytest .raises (KeyError , match = msg ):
138
133
idx .slice_locs (np .nan )
139
134
140
135
def test_get_loc_missing_nan (self ):
141
136
# GH#8569
142
- idx = Float64Index ([1 , 2 ])
137
+ idx = Index ([1 , 2 ], dtype = np . float64 )
143
138
assert idx .get_loc (1 ) == 0
144
139
with pytest .raises (KeyError , match = r"^3$" ):
145
140
idx .get_loc (3 )
@@ -285,14 +280,16 @@ def test_get_indexer_nearest_decreasing(self, method, expected):
285
280
actual = index .get_indexer ([0.2 , 1.8 , 8.5 ], method = method )
286
281
tm .assert_numpy_array_equal (actual , np .array (expected , dtype = np .intp ))
287
282
288
- @pytest .mark .parametrize (
289
- "idx_class" , [Int64Index , RangeIndex , Float64Index , UInt64Index ]
290
- )
283
+ @pytest .mark .parametrize ("idx_dtype" , ["int64" , "float64" , "uint64" , "range" ])
291
284
@pytest .mark .parametrize ("method" , ["get_indexer" , "get_indexer_non_unique" ])
292
- def test_get_indexer_numeric_index_boolean_target (self , method , idx_class ):
285
+ def test_get_indexer_numeric_index_boolean_target (self , method , idx_dtype ):
293
286
# GH 16877
294
287
295
- numeric_index = idx_class (RangeIndex (4 ))
288
+ if idx_dtype == "range" :
289
+ numeric_index = RangeIndex (4 )
290
+ else :
291
+ numeric_index = Index (np .arange (4 , dtype = idx_dtype ))
292
+
296
293
other = Index ([True , False , True ])
297
294
298
295
result = getattr (numeric_index , method )(other )
@@ -336,7 +333,7 @@ def test_get_indexer_numeric_vs_bool(self):
336
333
tm .assert_numpy_array_equal (res , expected )
337
334
338
335
def test_get_indexer_float64 (self ):
339
- idx = Float64Index ([0.0 , 1.0 , 2.0 ])
336
+ idx = Index ([0.0 , 1.0 , 2.0 ], dtype = np . float64 )
340
337
tm .assert_numpy_array_equal (
341
338
idx .get_indexer (idx ), np .array ([0 , 1 , 2 ], dtype = np .intp )
342
339
)
@@ -354,39 +351,39 @@ def test_get_indexer_float64(self):
354
351
355
352
def test_get_indexer_nan (self ):
356
353
# GH#7820
357
- result = Float64Index ([1 , 2 , np .nan ]).get_indexer ([np .nan ])
354
+ result = Index ([1 , 2 , np .nan ], dtype = np . float64 ).get_indexer ([np .nan ])
358
355
expected = np .array ([2 ], dtype = np .intp )
359
356
tm .assert_numpy_array_equal (result , expected )
360
357
361
358
def test_get_indexer_int64 (self ):
362
- index = Int64Index (range (0 , 20 , 2 ))
363
- target = Int64Index (np .arange (10 ))
359
+ index = Index (range (0 , 20 , 2 ), dtype = np . int64 )
360
+ target = Index (np .arange (10 ), dtype = np . int64 )
364
361
indexer = index .get_indexer (target )
365
362
expected = np .array ([0 , - 1 , 1 , - 1 , 2 , - 1 , 3 , - 1 , 4 , - 1 ], dtype = np .intp )
366
363
tm .assert_numpy_array_equal (indexer , expected )
367
364
368
- target = Int64Index (np .arange (10 ))
365
+ target = Index (np .arange (10 ), dtype = np . int64 )
369
366
indexer = index .get_indexer (target , method = "pad" )
370
367
expected = np .array ([0 , 0 , 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 ], dtype = np .intp )
371
368
tm .assert_numpy_array_equal (indexer , expected )
372
369
373
- target = Int64Index (np .arange (10 ))
370
+ target = Index (np .arange (10 ), dtype = np . int64 )
374
371
indexer = index .get_indexer (target , method = "backfill" )
375
372
expected = np .array ([0 , 1 , 1 , 2 , 2 , 3 , 3 , 4 , 4 , 5 ], dtype = np .intp )
376
373
tm .assert_numpy_array_equal (indexer , expected )
377
374
378
375
def test_get_indexer_uint64 (self , index_large ):
379
- target = UInt64Index (np .arange (10 ).astype ("uint64" ) * 5 + 2 ** 63 )
376
+ target = Index (np .arange (10 ).astype ("uint64" ) * 5 + 2 ** 63 )
380
377
indexer = index_large .get_indexer (target )
381
378
expected = np .array ([0 , - 1 , 1 , 2 , 3 , 4 , - 1 , - 1 , - 1 , - 1 ], dtype = np .intp )
382
379
tm .assert_numpy_array_equal (indexer , expected )
383
380
384
- target = UInt64Index (np .arange (10 ).astype ("uint64" ) * 5 + 2 ** 63 )
381
+ target = Index (np .arange (10 ).astype ("uint64" ) * 5 + 2 ** 63 )
385
382
indexer = index_large .get_indexer (target , method = "pad" )
386
383
expected = np .array ([0 , 0 , 1 , 2 , 3 , 4 , 4 , 4 , 4 , 4 ], dtype = np .intp )
387
384
tm .assert_numpy_array_equal (indexer , expected )
388
385
389
- target = UInt64Index (np .arange (10 ).astype ("uint64" ) * 5 + 2 ** 63 )
386
+ target = Index (np .arange (10 ).astype ("uint64" ) * 5 + 2 ** 63 )
390
387
indexer = index_large .get_indexer (target , method = "backfill" )
391
388
expected = np .array ([0 , 1 , 1 , 2 , 3 , 4 , - 1 , - 1 , - 1 , - 1 ], dtype = np .intp )
392
389
tm .assert_numpy_array_equal (indexer , expected )
@@ -396,9 +393,9 @@ class TestWhere:
396
393
@pytest .mark .parametrize (
397
394
"index" ,
398
395
[
399
- Float64Index (np .arange (5 , dtype = "float64" )),
400
- Int64Index (range (0 , 20 , 2 )),
401
- UInt64Index (np .arange (5 , dtype = "uint64" )),
396
+ Index (np .arange (5 , dtype = "float64" )),
397
+ Index (range (0 , 20 , 2 ), dtype = np . int64 ),
398
+ Index (np .arange (5 , dtype = "uint64" )),
402
399
],
403
400
)
404
401
def test_where (self , listlike_box , index ):
@@ -407,16 +404,16 @@ def test_where(self, listlike_box, index):
407
404
result = index .where (listlike_box (cond ))
408
405
409
406
cond = [False ] + [True ] * (len (index ) - 1 )
410
- expected = Float64Index ([index ._na_value ] + index [1 :].tolist ())
407
+ expected = Index ([index ._na_value ] + index [1 :].tolist (), dtype = np . float64 )
411
408
result = index .where (listlike_box (cond ))
412
409
tm .assert_index_equal (result , expected )
413
410
414
411
def test_where_uint64 (self ):
415
- idx = UInt64Index ([0 , 6 , 2 ])
412
+ idx = Index ([0 , 6 , 2 ], dtype = np . uint64 )
416
413
mask = np .array ([False , True , False ])
417
414
other = np .array ([1 ], dtype = np .int64 )
418
415
419
- expected = UInt64Index ([1 , 6 , 1 ])
416
+ expected = Index ([1 , 6 , 1 ], dtype = np . uint64 )
420
417
421
418
result = idx .where (mask , other )
422
419
tm .assert_index_equal (result , expected )
@@ -437,27 +434,27 @@ def test_where_infers_type_instead_of_trying_to_convert_string_to_float(self):
437
434
438
435
439
436
class TestTake :
440
- @pytest .mark .parametrize ("klass " , [Float64Index , Int64Index , UInt64Index ])
441
- def test_take_preserve_name (self , klass ):
442
- index = klass ([1 , 2 , 3 , 4 ], name = "foo" )
437
+ @pytest .mark .parametrize ("idx_dtype " , [np . float64 , np . int64 , np . uint64 ])
438
+ def test_take_preserve_name (self , idx_dtype ):
439
+ index = Index ([1 , 2 , 3 , 4 ], dtype = idx_dtype , name = "foo" )
443
440
taken = index .take ([3 , 0 , 1 ])
444
441
assert index .name == taken .name
445
442
446
443
def test_take_fill_value_float64 (self ):
447
444
# GH 12631
448
- idx = Float64Index ([1.0 , 2.0 , 3.0 ], name = "xxx" )
445
+ idx = Index ([1.0 , 2.0 , 3.0 ], name = "xxx" , dtype = np . float64 )
449
446
result = idx .take (np .array ([1 , 0 , - 1 ]))
450
- expected = Float64Index ([2.0 , 1.0 , 3.0 ], name = "xxx" )
447
+ expected = Index ([2.0 , 1.0 , 3.0 ], dtype = np . float64 , name = "xxx" )
451
448
tm .assert_index_equal (result , expected )
452
449
453
450
# fill_value
454
451
result = idx .take (np .array ([1 , 0 , - 1 ]), fill_value = True )
455
- expected = Float64Index ([2.0 , 1.0 , np .nan ], name = "xxx" )
452
+ expected = Index ([2.0 , 1.0 , np .nan ], dtype = np . float64 , name = "xxx" )
456
453
tm .assert_index_equal (result , expected )
457
454
458
455
# allow_fill=False
459
456
result = idx .take (np .array ([1 , 0 , - 1 ]), allow_fill = False , fill_value = True )
460
- expected = Float64Index ([2.0 , 1.0 , 3.0 ], name = "xxx" )
457
+ expected = Index ([2.0 , 1.0 , 3.0 ], dtype = np . float64 , name = "xxx" )
461
458
tm .assert_index_equal (result , expected )
462
459
463
460
msg = (
@@ -473,15 +470,15 @@ def test_take_fill_value_float64(self):
473
470
with pytest .raises (IndexError , match = msg ):
474
471
idx .take (np .array ([1 , - 5 ]))
475
472
476
- @pytest .mark .parametrize ("klass " , [Int64Index , UInt64Index ])
477
- def test_take_fill_value_ints (self , klass ):
473
+ @pytest .mark .parametrize ("dtype " , [np . int64 , np . uint64 ])
474
+ def test_take_fill_value_ints (self , dtype ):
478
475
# see gh-12631
479
- idx = klass ([1 , 2 , 3 ], name = "xxx" )
476
+ idx = Index ([1 , 2 , 3 ], dtype = dtype , name = "xxx" )
480
477
result = idx .take (np .array ([1 , 0 , - 1 ]))
481
- expected = klass ([2 , 1 , 3 ], name = "xxx" )
478
+ expected = Index ([2 , 1 , 3 ], dtype = dtype , name = "xxx" )
482
479
tm .assert_index_equal (result , expected )
483
480
484
- name = klass .__name__
481
+ name = type ( idx ) .__name__
485
482
msg = f"Unable to fill values because { name } cannot contain NA"
486
483
487
484
# fill_value=True
@@ -490,7 +487,7 @@ def test_take_fill_value_ints(self, klass):
490
487
491
488
# allow_fill=False
492
489
result = idx .take (np .array ([1 , 0 , - 1 ]), allow_fill = False , fill_value = True )
493
- expected = klass ([2 , 1 , 3 ], name = "xxx" )
490
+ expected = Index ([2 , 1 , 3 ], dtype = dtype , name = "xxx" )
494
491
tm .assert_index_equal (result , expected )
495
492
496
493
with pytest .raises (ValueError , match = msg ):
@@ -504,18 +501,18 @@ def test_take_fill_value_ints(self, klass):
504
501
505
502
506
503
class TestContains :
507
- @pytest .mark .parametrize ("klass " , [Float64Index , Int64Index , UInt64Index ])
508
- def test_contains_none (self , klass ):
504
+ @pytest .mark .parametrize ("dtype " , [np . float64 , np . int64 , np . uint64 ])
505
+ def test_contains_none (self , dtype ):
509
506
# GH#35788 should return False, not raise TypeError
510
- index = klass ([0 , 1 , 2 , 3 , 4 ])
507
+ index = Index ([0 , 1 , 2 , 3 , 4 ], dtype = dtype )
511
508
assert None not in index
512
509
513
510
def test_contains_float64_nans (self ):
514
- index = Float64Index ([1.0 , 2.0 , np .nan ])
511
+ index = Index ([1.0 , 2.0 , np .nan ], dtype = np . float64 )
515
512
assert np .nan in index
516
513
517
514
def test_contains_float64_not_nans (self ):
518
- index = Float64Index ([1.0 , 2.0 , np .nan ])
515
+ index = Index ([1.0 , 2.0 , np .nan ], dtype = np . float64 )
519
516
assert 1.0 in index
520
517
521
518
0 commit comments