diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index aaf58f1fcb150..40f23c25a1e99 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -30,9 +30,13 @@ from pandas.core.dtypes.common import ( is_datetime64_dtype, is_datetime64tz_dtype, + is_float_dtype, + is_integer_dtype, is_period_dtype, is_sequence, is_timedelta64_dtype, + is_unsigned_integer_dtype, + pandas_dtype, ) import pandas as pd @@ -41,11 +45,14 @@ CategoricalIndex, DataFrame, DatetimeIndex, + Float64Index, Index, + Int64Index, IntervalIndex, MultiIndex, RangeIndex, Series, + UInt64Index, bdate_range, ) from pandas._testing._io import ( # noqa:F401 @@ -292,12 +299,32 @@ def makeBoolIndex(k=10, name=None): return Index([False, True] + [False] * (k - 2), name=name) +def makeNumericIndex(k=10, name=None, *, dtype): + dtype = pandas_dtype(dtype) + assert isinstance(dtype, np.dtype) + + if is_integer_dtype(dtype): + values = np.arange(k, dtype=dtype) + if is_unsigned_integer_dtype(dtype): + values += 2 ** (dtype.itemsize * 8 - 1) + elif is_float_dtype(dtype): + values = np.random.random_sample(k) - np.random.random_sample(1) + values.sort() + values = values * (10 ** np.random.randint(0, 9)) + else: + raise NotImplementedError(f"wrong dtype {dtype}") + + return Index(values, dtype=dtype, name=name) + + def makeIntIndex(k=10, name=None): - return Index(list(range(k)), name=name) + base_idx = makeNumericIndex(k, name=name, dtype="int64") + return Int64Index(base_idx) def makeUIntIndex(k=10, name=None): - return Index([2 ** 63 + i for i in range(k)], name=name) + base_idx = makeNumericIndex(k, name=name, dtype="uint64") + return UInt64Index(base_idx) def makeRangeIndex(k=10, name=None, **kwargs): @@ -305,8 +332,8 @@ def makeRangeIndex(k=10, name=None, **kwargs): def makeFloatIndex(k=10, name=None): - values = sorted(np.random.random_sample(k)) - np.random.random_sample(1) - return Index(values * (10 ** np.random.randint(0, 9)), name=name) + base_idx = makeNumericIndex(k, name=name, dtype="float64") + return Float64Index(base_idx) def makeDateIndex(k: int = 10, freq="B", name=None, **kwargs) -> DatetimeIndex: diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index 2d695458e32e6..96d010b487a79 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -308,7 +308,7 @@ def assert_index_equal( """ __tracebackhide__ = True - def _check_types(left, right, obj="Index"): + def _check_types(left, right, obj="Index") -> None: if not exact: return diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index e6526bd0eaf2f..ea2d5d9eec6ac 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -106,20 +106,22 @@ def _can_hold_na(self) -> bool: else: return False - @cache_readonly + _engine_types: dict[np.dtype, type[libindex.IndexEngine]] = { + np.dtype(np.int8): libindex.Int8Engine, + np.dtype(np.int16): libindex.Int16Engine, + np.dtype(np.int32): libindex.Int32Engine, + np.dtype(np.int64): libindex.Int64Engine, + np.dtype(np.uint8): libindex.UInt8Engine, + np.dtype(np.uint16): libindex.UInt16Engine, + np.dtype(np.uint32): libindex.UInt32Engine, + np.dtype(np.uint64): libindex.UInt64Engine, + np.dtype(np.float32): libindex.Float32Engine, + np.dtype(np.float64): libindex.Float64Engine, + } + + @property def _engine_type(self): - return { - np.int8: libindex.Int8Engine, - np.int16: libindex.Int16Engine, - np.int32: libindex.Int32Engine, - np.int64: libindex.Int64Engine, - np.uint8: libindex.UInt8Engine, - np.uint16: libindex.UInt16Engine, - np.uint32: libindex.UInt32Engine, - np.uint64: libindex.UInt64Engine, - np.float32: libindex.Float32Engine, - np.float64: libindex.Float64Engine, - }[self.dtype.type] + return self._engine_types[self.dtype] @cache_readonly def inferred_type(self) -> str: diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py index c36552f59da71..8b7070e945439 100644 --- a/pandas/tests/api/test_api.py +++ b/pandas/tests/api/test_api.py @@ -214,7 +214,7 @@ def test_api(self): + self.funcs_to + self.private_modules ) - self.check(pd, checkthese, self.ignored) + self.check(namespace=pd, expected=checkthese, ignored=self.ignored) def test_depr(self): deprecated_list = ( diff --git a/pandas/tests/base/test_unique.py b/pandas/tests/base/test_unique.py index 26e785a2796b1..cabe766a4e9eb 100644 --- a/pandas/tests/base/test_unique.py +++ b/pandas/tests/base/test_unique.py @@ -23,12 +23,12 @@ def test_unique(index_or_series_obj): if isinstance(obj, pd.MultiIndex): expected = pd.MultiIndex.from_tuples(unique_values) expected.names = obj.names - tm.assert_index_equal(result, expected) + tm.assert_index_equal(result, expected, exact=True) elif isinstance(obj, pd.Index): expected = pd.Index(unique_values, dtype=obj.dtype) if is_datetime64tz_dtype(obj.dtype): expected = expected.normalize() - tm.assert_index_equal(result, expected) + tm.assert_index_equal(result, expected, exact=True) else: expected = np.array(unique_values) tm.assert_numpy_array_equal(result, expected) @@ -67,7 +67,7 @@ def test_unique_null(null_obj, index_or_series_obj): if is_datetime64tz_dtype(obj.dtype): result = result.normalize() expected = expected.normalize() - tm.assert_index_equal(result, expected) + tm.assert_index_equal(result, expected, exact=True) else: expected = np.array(unique_values, dtype=obj.dtype) tm.assert_numpy_array_equal(result, expected) @@ -118,7 +118,7 @@ def test_unique_bad_unicode(idx_or_series_w_bad_unicode): if isinstance(obj, pd.Index): expected = pd.Index(["\ud83d"], dtype=object) - tm.assert_index_equal(result, expected) + tm.assert_index_equal(result, expected, exact=True) else: expected = np.array(["\ud83d"], dtype=object) tm.assert_numpy_array_equal(result, expected)