1
1
import numpy as np
2
2
import pytest
3
3
4
- from pandas ._libs .tslibs import Timestamp
5
-
6
4
import pandas as pd
7
5
from pandas import (
8
6
Index ,
9
7
Series ,
10
8
)
11
9
import pandas ._testing as tm
12
- from pandas .core .indexes .api import NumericIndex
13
10
from pandas .tests .indexes .common import NumericBase
14
11
15
12
16
13
class TestFloatNumericIndex (NumericBase ):
17
- _index_cls = NumericIndex
14
+ _index_cls = Index
18
15
19
16
@pytest .fixture (params = [np .float64 , np .float32 ])
20
17
def dtype (self , request ):
21
18
return request .param
22
19
23
- @pytest .fixture (params = ["category" , "datetime64" , "object" ])
24
- def invalid_dtype (self , request ):
25
- return request .param
26
-
27
20
@pytest .fixture
28
21
def simple_index (self , dtype ):
29
22
values = np .arange (5 , dtype = dtype )
@@ -50,19 +43,17 @@ def float_index(self, dtype):
50
43
return self ._index_cls ([0.0 , 2.5 , 5.0 , 7.5 , 10.0 ], dtype = dtype )
51
44
52
45
def test_repr_roundtrip (self , index ):
53
- tm . assert_index_equal ( eval ( repr ( index )), index , exact = True )
46
+ from pandas . core . api import NumericIndex # noqa: F401
54
47
55
- def check_is_index (self , idx ):
56
- assert isinstance (idx , Index )
57
- assert not isinstance (idx , self ._index_cls )
48
+ tm .assert_index_equal (eval (repr (index )), index , exact = True )
58
49
59
50
def check_coerce (self , a , b , is_float_index = True ):
60
51
assert a .equals (b )
61
52
tm .assert_index_equal (a , b , exact = False )
62
53
if is_float_index :
63
54
assert isinstance (b , self ._index_cls )
64
55
else :
65
- self . check_is_index (b )
56
+ assert type (b ) is Index
66
57
67
58
def test_constructor_from_list_no_dtype (self ):
68
59
index = self ._index_cls ([1.5 , 2.5 , 3.5 ])
@@ -110,7 +101,6 @@ def test_constructor(self, dtype):
110
101
def test_constructor_invalid (self ):
111
102
index_cls = self ._index_cls
112
103
cls_name = index_cls .__name__
113
-
114
104
# invalid
115
105
msg = (
116
106
rf"{ cls_name } \(\.\.\.\) must be called with a collection of "
@@ -119,13 +109,6 @@ def test_constructor_invalid(self):
119
109
with pytest .raises (TypeError , match = msg ):
120
110
index_cls (0.0 )
121
111
122
- msg = f"data is not compatible with { index_cls .__name__ } "
123
- with pytest .raises (ValueError , match = msg ):
124
- index_cls (["a" , "b" , 0.0 ])
125
-
126
- with pytest .raises (ValueError , match = msg ):
127
- index_cls ([Timestamp ("20130101" )])
128
-
129
112
def test_constructor_coerce (self , mixed_index , float_index ):
130
113
131
114
self .check_coerce (mixed_index , Index ([1.5 , 2 , 3 , 4 , 5 ]))
@@ -254,6 +237,8 @@ def test_fillna_float64(self):
254
237
255
238
256
239
class NumericInt (NumericBase ):
240
+ _index_cls = Index
241
+
257
242
def test_is_monotonic (self ):
258
243
index_cls = self ._index_cls
259
244
@@ -317,18 +302,13 @@ def test_identical(self, simple_index, dtype):
317
302
318
303
assert not index .astype (dtype = object ).identical (index .astype (dtype = dtype ))
319
304
320
- def test_cant_or_shouldnt_cast (self ):
321
- msg = f"data is not compatible with { self . _index_cls . __name__ } "
305
+ def test_cant_or_shouldnt_cast (self , dtype ):
306
+ msg = r"invalid literal for int\(\) with base 10: 'foo' "
322
307
323
308
# can't
324
309
data = ["foo" , "bar" , "baz" ]
325
310
with pytest .raises (ValueError , match = msg ):
326
- self ._index_cls (data )
327
-
328
- # shouldn't
329
- data = ["0" , "1" , "2" ]
330
- with pytest .raises (ValueError , match = msg ):
331
- self ._index_cls (data )
311
+ self ._index_cls (data , dtype = dtype )
332
312
333
313
def test_view_index (self , simple_index ):
334
314
index = simple_index
@@ -341,16 +321,10 @@ def test_prevent_casting(self, simple_index):
341
321
342
322
343
323
class TestIntNumericIndex (NumericInt ):
344
- _index_cls = NumericIndex
345
-
346
324
@pytest .fixture (params = [np .int64 , np .int32 , np .int16 , np .int8 ])
347
325
def dtype (self , request ):
348
326
return request .param
349
327
350
- @pytest .fixture (params = ["category" , "datetime64" , "object" ])
351
- def invalid_dtype (self , request ):
352
- return request .param
353
-
354
328
@pytest .fixture
355
329
def simple_index (self , dtype ):
356
330
return self ._index_cls (range (0 , 20 , 2 ), dtype = dtype )
@@ -427,7 +401,8 @@ def test_constructor_corner(self, dtype):
427
401
428
402
# preventing casting
429
403
arr = np .array ([1 , "2" , 3 , "4" ], dtype = object )
430
- with pytest .raises (TypeError , match = "casting" ):
404
+ msg = "Trying to coerce float values to integers"
405
+ with pytest .raises (ValueError , match = msg ):
431
406
index_cls (arr , dtype = dtype )
432
407
433
408
def test_constructor_coercion_signed_to_unsigned (
@@ -468,7 +443,7 @@ def test_coerce_list(self):
468
443
class TestFloat16Index :
469
444
# float 16 indexes not supported
470
445
# GH 49535
471
- _index_cls = NumericIndex
446
+ _index_cls = Index
472
447
473
448
def test_constructor (self ):
474
449
index_cls = self ._index_cls
@@ -504,17 +479,10 @@ def test_constructor(self):
504
479
505
480
506
481
class TestUIntNumericIndex (NumericInt ):
507
-
508
- _index_cls = NumericIndex
509
-
510
482
@pytest .fixture (params = [np .uint64 ])
511
483
def dtype (self , request ):
512
484
return request .param
513
485
514
- @pytest .fixture (params = ["category" , "datetime64" , "object" ])
515
- def invalid_dtype (self , request ):
516
- return request .param
517
-
518
486
@pytest .fixture
519
487
def simple_index (self , dtype ):
520
488
# compat with shared Int64/Float64 tests
@@ -583,8 +551,8 @@ def test_map_dtype_inference_unsigned_to_signed():
583
551
584
552
def test_map_dtype_inference_overflows ():
585
553
# GH#44609 case where we have to upcast
586
- idx = NumericIndex (np .array ([1 , 2 , 3 ], dtype = np .int8 ))
554
+ idx = Index (np .array ([1 , 2 , 3 ], dtype = np .int8 ))
587
555
result = idx .map (lambda x : x * 1000 )
588
556
# TODO: we could plausibly try to infer down to int16 here
589
- expected = NumericIndex ([1000 , 2000 , 3000 ], dtype = np .int64 )
557
+ expected = Index ([1000 , 2000 , 3000 ], dtype = np .int64 )
590
558
tm .assert_index_equal (result , expected )
0 commit comments