From c6657c598bd42be20e946d78f331af5ba47d74a8 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Sat, 19 Nov 2022 09:46:32 +0000 Subject: [PATCH 1/3] TST: test UInt64Index in tests/indexes/interval/test_constructors.py --- .../indexes/interval/test_constructors.py | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexes/interval/test_constructors.py b/pandas/tests/indexes/interval/test_constructors.py index ce0de97befec3..cca4316770a90 100644 --- a/pandas/tests/indexes/interval/test_constructors.py +++ b/pandas/tests/indexes/interval/test_constructors.py @@ -18,9 +18,11 @@ timedelta_range, ) import pandas._testing as tm +from pandas.api.types import is_unsigned_integer_dtype from pandas.core.api import ( Float64Index, Int64Index, + UInt64Index, ) from pandas.core.arrays import IntervalArray import pandas.core.common as com @@ -38,25 +40,36 @@ class ConstructorTests: get_kwargs_from_breaks to the expected format. """ + # get_kwargs_from_breaks in TestFromTuples and TestClassconstructors just return + # tuples of ints, so IntervalIndex can't know the original dtype was uint + _use_dtype_in_test_constructor_uint = False + @pytest.mark.parametrize( "breaks", [ [3, 14, 15, 92, 653], np.arange(10, dtype="int64"), Int64Index(range(-10, 11)), + UInt64Index(range(10, 31)), Float64Index(np.arange(20, 30, 0.5)), date_range("20180101", periods=10), date_range("20180101", periods=10, tz="US/Eastern"), timedelta_range("1 day", periods=10), ], ) - def test_constructor(self, constructor, breaks, closed, name): + @pytest.mark.parametrize("use_dtype", [True, False]) + def test_constructor(self, constructor, breaks, closed, name, use_dtype): + breaks_dtype = getattr(breaks, "dtype", "int64") result_kwargs = self.get_kwargs_from_breaks(breaks, closed) + is_uint = is_unsigned_integer_dtype(breaks_dtype) + if use_dtype or (self._use_dtype_in_test_constructor_uint and is_uint): + result_kwargs["dtype"] = IntervalDtype(breaks_dtype) + result = constructor(closed=closed, name=name, **result_kwargs) assert result.closed == closed assert result.name == name - assert result.dtype.subtype == getattr(breaks, "dtype", "int64") + assert result.dtype.subtype == breaks_dtype tm.assert_index_equal(result.left, Index(breaks[:-1])) tm.assert_index_equal(result.right, Index(breaks[1:])) @@ -86,8 +99,7 @@ def test_constructor_dtype(self, constructor, breaks, subtype): "breaks", [ Int64Index([0, 1, 2, 3, 4]), - Int64Index([0, 1, 2, 3, 4]), - Int64Index([0, 1, 2, 3, 4]), + UInt64Index([0, 1, 2, 3, 4]), Float64Index([0, 1, 2, 3, 4]), date_range("2017-01-01", periods=5), timedelta_range("1 day", periods=5), @@ -123,6 +135,7 @@ def test_constructor_nan(self, constructor, breaks, closed): [ [], np.array([], dtype="int64"), + np.array([], dtype="uint64"), np.array([], dtype="float64"), np.array([], dtype="datetime64[ns]"), np.array([], dtype="timedelta64[ns]"), @@ -294,6 +307,8 @@ def test_left_right_dont_share_data(self): class TestFromTuples(ConstructorTests): """Tests specific to IntervalIndex.from_tuples""" + _use_dtype_in_test_constructor_uint = True + @pytest.fixture def constructor(self): return IntervalIndex.from_tuples @@ -341,6 +356,8 @@ def test_na_tuples(self): class TestClassConstructors(ConstructorTests): """Tests specific to the IntervalIndex/Index constructors""" + _use_dtype_in_test_constructor_uint = True + @pytest.fixture( params=[IntervalIndex, partial(Index, dtype="interval")], ids=["IntervalIndex", "Index"], From cf296fd6be35706195572a7c2dce6b40922904f0 Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Sat, 19 Nov 2022 11:09:02 +0000 Subject: [PATCH 2/3] add closed to IntervalDtype creation --- pandas/tests/indexes/interval/test_constructors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexes/interval/test_constructors.py b/pandas/tests/indexes/interval/test_constructors.py index cca4316770a90..8dba8e074704d 100644 --- a/pandas/tests/indexes/interval/test_constructors.py +++ b/pandas/tests/indexes/interval/test_constructors.py @@ -63,7 +63,7 @@ def test_constructor(self, constructor, breaks, closed, name, use_dtype): result_kwargs = self.get_kwargs_from_breaks(breaks, closed) is_uint = is_unsigned_integer_dtype(breaks_dtype) if use_dtype or (self._use_dtype_in_test_constructor_uint and is_uint): - result_kwargs["dtype"] = IntervalDtype(breaks_dtype) + result_kwargs["dtype"] = IntervalDtype(breaks_dtype, closed=closed) result = constructor(closed=closed, name=name, **result_kwargs) From 2153c84260900b865e67a6ff43ea49e17515c68b Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Sun, 20 Nov 2022 07:38:49 +0000 Subject: [PATCH 3/3] make skip options more flexible --- .../indexes/interval/test_constructors.py | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/pandas/tests/indexes/interval/test_constructors.py b/pandas/tests/indexes/interval/test_constructors.py index 8dba8e074704d..1c8697e96c2e9 100644 --- a/pandas/tests/indexes/interval/test_constructors.py +++ b/pandas/tests/indexes/interval/test_constructors.py @@ -40,9 +40,10 @@ class ConstructorTests: get_kwargs_from_breaks to the expected format. """ - # get_kwargs_from_breaks in TestFromTuples and TestClassconstructors just return - # tuples of ints, so IntervalIndex can't know the original dtype was uint - _use_dtype_in_test_constructor_uint = False + def _skip_test_constructor(self, dtype): + # get_kwargs_from_breaks in TestFromTuples and TestClassconstructors just return + # tuples of ints, so IntervalIndex can't know the original dtype + return False, "" @pytest.mark.parametrize( "breaks", @@ -60,9 +61,13 @@ class ConstructorTests: @pytest.mark.parametrize("use_dtype", [True, False]) def test_constructor(self, constructor, breaks, closed, name, use_dtype): breaks_dtype = getattr(breaks, "dtype", "int64") + + skip, skip_msg = self._skip_test_constructor(breaks_dtype) + if skip: + pytest.skip(skip_msg) + result_kwargs = self.get_kwargs_from_breaks(breaks, closed) - is_uint = is_unsigned_integer_dtype(breaks_dtype) - if use_dtype or (self._use_dtype_in_test_constructor_uint and is_uint): + if use_dtype: result_kwargs["dtype"] = IntervalDtype(breaks_dtype, closed=closed) result = constructor(closed=closed, name=name, **result_kwargs) @@ -307,7 +312,11 @@ def test_left_right_dont_share_data(self): class TestFromTuples(ConstructorTests): """Tests specific to IntervalIndex.from_tuples""" - _use_dtype_in_test_constructor_uint = True + def _skip_test_constructor(self, dtype): + if is_unsigned_integer_dtype(dtype): + return True, "tuples don't have a dtype" + else: + return False, "" @pytest.fixture def constructor(self): @@ -356,7 +365,13 @@ def test_na_tuples(self): class TestClassConstructors(ConstructorTests): """Tests specific to the IntervalIndex/Index constructors""" - _use_dtype_in_test_constructor_uint = True + def _skip_test_constructor(self, dtype): + # get_kwargs_from_breaks in TestFromTuples and TestClassconstructors just return + # tuples of ints, so IntervalIndex can't know the original dtype + if is_unsigned_integer_dtype(dtype): + return True, "tuples don't have a dtype" + else: + return False, "" @pytest.fixture( params=[IntervalIndex, partial(Index, dtype="interval")],