17
17
18
18
class TestFloat64Index (NumericBase ):
19
19
_index_cls = Float64Index
20
- _dtype = np .float64
20
+
21
+ @pytest .fixture
22
+ def dtype (self , request ):
23
+ return np .float64
21
24
22
25
@pytest .fixture (
23
26
params = ["int64" , "uint64" , "category" , "datetime64" ],
@@ -26,8 +29,8 @@ def invalid_dtype(self, request):
26
29
return request .param
27
30
28
31
@pytest .fixture
29
- def simple_index (self ) -> Index :
30
- values = np .arange (5 , dtype = self . _dtype )
32
+ def simple_index (self , dtype ) -> Index :
33
+ values = np .arange (5 , dtype = dtype )
31
34
return self ._index_cls (values )
32
35
33
36
@pytest .fixture (
@@ -65,9 +68,8 @@ def check_coerce(self, a, b, is_float_index=True):
65
68
else :
66
69
self .check_is_index (b )
67
70
68
- def test_constructor (self ):
71
+ def test_constructor (self , dtype ):
69
72
index_cls = self ._index_cls
70
- dtype = self ._dtype
71
73
72
74
# explicit construction
73
75
index = index_cls ([1 , 2 , 3 , 4 , 5 ])
@@ -204,8 +206,7 @@ def test_equals_numeric_other_index_type(self, other):
204
206
pd .timedelta_range ("1 Day" , periods = 3 ),
205
207
],
206
208
)
207
- def test_lookups_datetimelike_values (self , vals ):
208
- dtype = self ._dtype
209
+ def test_lookups_datetimelike_values (self , vals , dtype ):
209
210
210
211
# If we have datetime64 or timedelta64 values, make sure they are
211
212
# wrappped correctly GH#31163
@@ -277,14 +278,14 @@ def test_fillna_float64(self):
277
278
278
279
279
280
class NumericInt (NumericBase ):
280
- def test_view (self ):
281
+ def test_view (self , dtype ):
281
282
index_cls = self ._index_cls
282
283
283
284
idx = index_cls ([], name = "Foo" )
284
285
idx_view = idx .view ()
285
286
assert idx_view .name == "Foo"
286
287
287
- idx_view = idx .view (self . _dtype )
288
+ idx_view = idx .view (dtype )
288
289
tm .assert_index_equal (idx , index_cls (idx_view , name = "Foo" ))
289
290
290
291
idx_view = idx .view (index_cls )
@@ -334,7 +335,7 @@ def test_logical_compat(self, simple_index):
334
335
assert idx .all () == idx .values .all ()
335
336
assert idx .any () == idx .values .any ()
336
337
337
- def test_identical (self , simple_index ):
338
+ def test_identical (self , simple_index , dtype ):
338
339
index = simple_index
339
340
340
341
idx = Index (index .copy ())
@@ -351,7 +352,7 @@ def test_identical(self, simple_index):
351
352
assert not idx .identical (index )
352
353
assert Index (same_values , name = "foo" , dtype = object ).identical (idx )
353
354
354
- assert not index .astype (dtype = object ).identical (index .astype (dtype = self . _dtype ))
355
+ assert not index .astype (dtype = object ).identical (index .astype (dtype = dtype ))
355
356
356
357
def test_cant_or_shouldnt_cast (self ):
357
358
msg = (
@@ -380,7 +381,10 @@ def test_prevent_casting(self, simple_index):
380
381
381
382
class TestInt64Index (NumericInt ):
382
383
_index_cls = Int64Index
383
- _dtype = np .int64
384
+
385
+ @pytest .fixture
386
+ def dtype (self ):
387
+ return np .int64
384
388
385
389
@pytest .fixture (
386
390
params = ["uint64" , "float64" , "category" , "datetime64" ],
@@ -389,18 +393,17 @@ def invalid_dtype(self, request):
389
393
return request .param
390
394
391
395
@pytest .fixture
392
- def simple_index (self ) -> Index :
393
- return self ._index_cls (range (0 , 20 , 2 ), dtype = self . _dtype )
396
+ def simple_index (self , dtype ) -> Index :
397
+ return self ._index_cls (range (0 , 20 , 2 ), dtype = dtype )
394
398
395
399
@pytest .fixture (
396
400
params = [range (0 , 20 , 2 ), range (19 , - 1 , - 1 )], ids = ["index_inc" , "index_dec" ]
397
401
)
398
402
def index (self , request ):
399
403
return self ._index_cls (request .param )
400
404
401
- def test_constructor (self ):
405
+ def test_constructor (self , dtype ):
402
406
index_cls = self ._index_cls
403
- dtype = self ._dtype
404
407
405
408
# pass list, coerce fine
406
409
index = index_cls ([- 5 , 0 , 1 , 2 ])
@@ -439,9 +442,8 @@ def test_constructor(self):
439
442
]:
440
443
tm .assert_index_equal (idx , expected )
441
444
442
- def test_constructor_corner (self ):
445
+ def test_constructor_corner (self , dtype ):
443
446
index_cls = self ._index_cls
444
- dtype = self ._dtype
445
447
446
448
arr = np .array ([1 , 2 , 3 , 4 ], dtype = object )
447
449
index = index_cls (arr )
@@ -465,26 +467,23 @@ def test_constructor_coercion_signed_to_unsigned(self, uint_dtype):
465
467
with pytest .raises (OverflowError , match = msg ):
466
468
Index ([- 1 ], dtype = uint_dtype )
467
469
468
- def test_constructor_unwraps_index (self ):
469
- idx = Index ([1 , 2 ])
470
- result = self ._index_cls (idx )
471
- expected = np .array ([1 , 2 ], dtype = self ._dtype )
472
- tm .assert_numpy_array_equal (result ._data , expected )
473
-
474
470
def test_coerce_list (self ):
475
471
# coerce things
476
472
arr = Index ([1 , 2 , 3 , 4 ])
477
473
assert isinstance (arr , self ._index_cls )
478
474
479
475
# but not if explicit dtype passed
480
476
arr = Index ([1 , 2 , 3 , 4 ], dtype = object )
481
- assert isinstance (arr , Index )
477
+ assert type (arr ) is Index
482
478
483
479
484
480
class TestUInt64Index (NumericInt ):
485
481
486
482
_index_cls = UInt64Index
487
- _dtype = np .uint64
483
+
484
+ @pytest .fixture
485
+ def dtype (self ):
486
+ return np .uint64
488
487
489
488
@pytest .fixture (
490
489
params = ["int64" , "float64" , "category" , "datetime64" ],
@@ -493,9 +492,9 @@ def invalid_dtype(self, request):
493
492
return request .param
494
493
495
494
@pytest .fixture
496
- def simple_index (self ) -> Index :
495
+ def simple_index (self , dtype ) -> Index :
497
496
# compat with shared Int64/Float64 tests
498
- return self ._index_cls (np .arange (5 , dtype = self . _dtype ))
497
+ return self ._index_cls (np .arange (5 , dtype = dtype ))
499
498
500
499
@pytest .fixture (
501
500
params = [
@@ -507,9 +506,8 @@ def simple_index(self) -> Index:
507
506
def index (self , request ):
508
507
return self ._index_cls (request .param )
509
508
510
- def test_constructor (self ):
509
+ def test_constructor (self , dtype ):
511
510
index_cls = self ._index_cls
512
- dtype = self ._dtype
513
511
514
512
idx = index_cls ([1 , 2 , 3 ])
515
513
res = Index ([1 , 2 , 3 ], dtype = dtype )
0 commit comments