Skip to content

Commit c6657c5

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
TST: test UInt64Index in tests/indexes/interval/test_constructors.py
1 parent 42bb8fe commit c6657c5

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

pandas/tests/indexes/interval/test_constructors.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
timedelta_range,
1919
)
2020
import pandas._testing as tm
21+
from pandas.api.types import is_unsigned_integer_dtype
2122
from pandas.core.api import (
2223
Float64Index,
2324
Int64Index,
25+
UInt64Index,
2426
)
2527
from pandas.core.arrays import IntervalArray
2628
import pandas.core.common as com
@@ -38,25 +40,36 @@ class ConstructorTests:
3840
get_kwargs_from_breaks to the expected format.
3941
"""
4042

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+
4147
@pytest.mark.parametrize(
4248
"breaks",
4349
[
4450
[3, 14, 15, 92, 653],
4551
np.arange(10, dtype="int64"),
4652
Int64Index(range(-10, 11)),
53+
UInt64Index(range(10, 31)),
4754
Float64Index(np.arange(20, 30, 0.5)),
4855
date_range("20180101", periods=10),
4956
date_range("20180101", periods=10, tz="US/Eastern"),
5057
timedelta_range("1 day", periods=10),
5158
],
5259
)
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")
5463
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+
5568
result = constructor(closed=closed, name=name, **result_kwargs)
5669

5770
assert result.closed == closed
5871
assert result.name == name
59-
assert result.dtype.subtype == getattr(breaks, "dtype", "int64")
72+
assert result.dtype.subtype == breaks_dtype
6073
tm.assert_index_equal(result.left, Index(breaks[:-1]))
6174
tm.assert_index_equal(result.right, Index(breaks[1:]))
6275

@@ -86,8 +99,7 @@ def test_constructor_dtype(self, constructor, breaks, subtype):
8699
"breaks",
87100
[
88101
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]),
91103
Float64Index([0, 1, 2, 3, 4]),
92104
date_range("2017-01-01", periods=5),
93105
timedelta_range("1 day", periods=5),
@@ -123,6 +135,7 @@ def test_constructor_nan(self, constructor, breaks, closed):
123135
[
124136
[],
125137
np.array([], dtype="int64"),
138+
np.array([], dtype="uint64"),
126139
np.array([], dtype="float64"),
127140
np.array([], dtype="datetime64[ns]"),
128141
np.array([], dtype="timedelta64[ns]"),
@@ -294,6 +307,8 @@ def test_left_right_dont_share_data(self):
294307
class TestFromTuples(ConstructorTests):
295308
"""Tests specific to IntervalIndex.from_tuples"""
296309

310+
_use_dtype_in_test_constructor_uint = True
311+
297312
@pytest.fixture
298313
def constructor(self):
299314
return IntervalIndex.from_tuples
@@ -341,6 +356,8 @@ def test_na_tuples(self):
341356
class TestClassConstructors(ConstructorTests):
342357
"""Tests specific to the IntervalIndex/Index constructors"""
343358

359+
_use_dtype_in_test_constructor_uint = True
360+
344361
@pytest.fixture(
345362
params=[IntervalIndex, partial(Index, dtype="interval")],
346363
ids=["IntervalIndex", "Index"],

0 commit comments

Comments
 (0)