18
18
timedelta_range ,
19
19
)
20
20
import pandas ._testing as tm
21
+ from pandas .api .types import is_unsigned_integer_dtype
21
22
from pandas .core .api import (
22
23
Float64Index ,
23
24
Int64Index ,
25
+ UInt64Index ,
24
26
)
25
27
from pandas .core .arrays import IntervalArray
26
28
import pandas .core .common as com
@@ -38,25 +40,36 @@ class ConstructorTests:
38
40
get_kwargs_from_breaks to the expected format.
39
41
"""
40
42
43
+ # get_kwargs_from_breaks in TestFromTuples and TestClassconstructors just return
44
+ # tuples of ints, so IntervalIndex can't know the original dtype was uint
45
+ _use_dtype_in_test_constructor_uint = False
46
+
41
47
@pytest .mark .parametrize (
42
48
"breaks" ,
43
49
[
44
50
[3 , 14 , 15 , 92 , 653 ],
45
51
np .arange (10 , dtype = "int64" ),
46
52
Int64Index (range (- 10 , 11 )),
53
+ UInt64Index (range (10 , 31 )),
47
54
Float64Index (np .arange (20 , 30 , 0.5 )),
48
55
date_range ("20180101" , periods = 10 ),
49
56
date_range ("20180101" , periods = 10 , tz = "US/Eastern" ),
50
57
timedelta_range ("1 day" , periods = 10 ),
51
58
],
52
59
)
53
- def test_constructor (self , constructor , breaks , closed , name ):
60
+ @pytest .mark .parametrize ("use_dtype" , [True , False ])
61
+ def test_constructor (self , constructor , breaks , closed , name , use_dtype ):
62
+ breaks_dtype = getattr (breaks , "dtype" , "int64" )
54
63
result_kwargs = self .get_kwargs_from_breaks (breaks , closed )
64
+ is_uint = is_unsigned_integer_dtype (breaks_dtype )
65
+ if use_dtype or (self ._use_dtype_in_test_constructor_uint and is_uint ):
66
+ result_kwargs ["dtype" ] = IntervalDtype (breaks_dtype )
67
+
55
68
result = constructor (closed = closed , name = name , ** result_kwargs )
56
69
57
70
assert result .closed == closed
58
71
assert result .name == name
59
- assert result .dtype .subtype == getattr ( breaks , "dtype" , "int64" )
72
+ assert result .dtype .subtype == breaks_dtype
60
73
tm .assert_index_equal (result .left , Index (breaks [:- 1 ]))
61
74
tm .assert_index_equal (result .right , Index (breaks [1 :]))
62
75
@@ -86,8 +99,7 @@ def test_constructor_dtype(self, constructor, breaks, subtype):
86
99
"breaks" ,
87
100
[
88
101
Int64Index ([0 , 1 , 2 , 3 , 4 ]),
89
- Int64Index ([0 , 1 , 2 , 3 , 4 ]),
90
- Int64Index ([0 , 1 , 2 , 3 , 4 ]),
102
+ UInt64Index ([0 , 1 , 2 , 3 , 4 ]),
91
103
Float64Index ([0 , 1 , 2 , 3 , 4 ]),
92
104
date_range ("2017-01-01" , periods = 5 ),
93
105
timedelta_range ("1 day" , periods = 5 ),
@@ -123,6 +135,7 @@ def test_constructor_nan(self, constructor, breaks, closed):
123
135
[
124
136
[],
125
137
np .array ([], dtype = "int64" ),
138
+ np .array ([], dtype = "uint64" ),
126
139
np .array ([], dtype = "float64" ),
127
140
np .array ([], dtype = "datetime64[ns]" ),
128
141
np .array ([], dtype = "timedelta64[ns]" ),
@@ -294,6 +307,8 @@ def test_left_right_dont_share_data(self):
294
307
class TestFromTuples (ConstructorTests ):
295
308
"""Tests specific to IntervalIndex.from_tuples"""
296
309
310
+ _use_dtype_in_test_constructor_uint = True
311
+
297
312
@pytest .fixture
298
313
def constructor (self ):
299
314
return IntervalIndex .from_tuples
@@ -341,6 +356,8 @@ def test_na_tuples(self):
341
356
class TestClassConstructors (ConstructorTests ):
342
357
"""Tests specific to the IntervalIndex/Index constructors"""
343
358
359
+ _use_dtype_in_test_constructor_uint = True
360
+
344
361
@pytest .fixture (
345
362
params = [IntervalIndex , partial (Index , dtype = "interval" )],
346
363
ids = ["IntervalIndex" , "Index" ],
0 commit comments