Skip to content

Commit c9d50d8

Browse files
authored
CLN: various related to numeric indexes (#41615)
1 parent fcacba5 commit c9d50d8

File tree

5 files changed

+52
-23
lines changed

5 files changed

+52
-23
lines changed

pandas/_testing/__init__.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030
from pandas.core.dtypes.common import (
3131
is_datetime64_dtype,
3232
is_datetime64tz_dtype,
33+
is_float_dtype,
34+
is_integer_dtype,
3335
is_period_dtype,
3436
is_sequence,
3537
is_timedelta64_dtype,
38+
is_unsigned_integer_dtype,
39+
pandas_dtype,
3640
)
3741

3842
import pandas as pd
@@ -41,11 +45,14 @@
4145
CategoricalIndex,
4246
DataFrame,
4347
DatetimeIndex,
48+
Float64Index,
4449
Index,
50+
Int64Index,
4551
IntervalIndex,
4652
MultiIndex,
4753
RangeIndex,
4854
Series,
55+
UInt64Index,
4956
bdate_range,
5057
)
5158
from pandas._testing._io import ( # noqa:F401
@@ -292,21 +299,41 @@ def makeBoolIndex(k=10, name=None):
292299
return Index([False, True] + [False] * (k - 2), name=name)
293300

294301

302+
def makeNumericIndex(k=10, name=None, *, dtype):
303+
dtype = pandas_dtype(dtype)
304+
assert isinstance(dtype, np.dtype)
305+
306+
if is_integer_dtype(dtype):
307+
values = np.arange(k, dtype=dtype)
308+
if is_unsigned_integer_dtype(dtype):
309+
values += 2 ** (dtype.itemsize * 8 - 1)
310+
elif is_float_dtype(dtype):
311+
values = np.random.random_sample(k) - np.random.random_sample(1)
312+
values.sort()
313+
values = values * (10 ** np.random.randint(0, 9))
314+
else:
315+
raise NotImplementedError(f"wrong dtype {dtype}")
316+
317+
return Index(values, dtype=dtype, name=name)
318+
319+
295320
def makeIntIndex(k=10, name=None):
296-
return Index(list(range(k)), name=name)
321+
base_idx = makeNumericIndex(k, name=name, dtype="int64")
322+
return Int64Index(base_idx)
297323

298324

299325
def makeUIntIndex(k=10, name=None):
300-
return Index([2 ** 63 + i for i in range(k)], name=name)
326+
base_idx = makeNumericIndex(k, name=name, dtype="uint64")
327+
return UInt64Index(base_idx)
301328

302329

303330
def makeRangeIndex(k=10, name=None, **kwargs):
304331
return RangeIndex(0, k, 1, name=name, **kwargs)
305332

306333

307334
def makeFloatIndex(k=10, name=None):
308-
values = sorted(np.random.random_sample(k)) - np.random.random_sample(1)
309-
return Index(values * (10 ** np.random.randint(0, 9)), name=name)
335+
base_idx = makeNumericIndex(k, name=name, dtype="float64")
336+
return Float64Index(base_idx)
310337

311338

312339
def makeDateIndex(k: int = 10, freq="B", name=None, **kwargs) -> DatetimeIndex:

pandas/_testing/asserters.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def assert_index_equal(
308308
"""
309309
__tracebackhide__ = True
310310

311-
def _check_types(left, right, obj="Index"):
311+
def _check_types(left, right, obj="Index") -> None:
312312
if not exact:
313313
return
314314

pandas/core/indexes/numeric.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,22 @@ def _can_hold_na(self) -> bool:
106106
else:
107107
return False
108108

109-
@cache_readonly
109+
_engine_types: dict[np.dtype, type[libindex.IndexEngine]] = {
110+
np.dtype(np.int8): libindex.Int8Engine,
111+
np.dtype(np.int16): libindex.Int16Engine,
112+
np.dtype(np.int32): libindex.Int32Engine,
113+
np.dtype(np.int64): libindex.Int64Engine,
114+
np.dtype(np.uint8): libindex.UInt8Engine,
115+
np.dtype(np.uint16): libindex.UInt16Engine,
116+
np.dtype(np.uint32): libindex.UInt32Engine,
117+
np.dtype(np.uint64): libindex.UInt64Engine,
118+
np.dtype(np.float32): libindex.Float32Engine,
119+
np.dtype(np.float64): libindex.Float64Engine,
120+
}
121+
122+
@property
110123
def _engine_type(self):
111-
return {
112-
np.int8: libindex.Int8Engine,
113-
np.int16: libindex.Int16Engine,
114-
np.int32: libindex.Int32Engine,
115-
np.int64: libindex.Int64Engine,
116-
np.uint8: libindex.UInt8Engine,
117-
np.uint16: libindex.UInt16Engine,
118-
np.uint32: libindex.UInt32Engine,
119-
np.uint64: libindex.UInt64Engine,
120-
np.float32: libindex.Float32Engine,
121-
np.float64: libindex.Float64Engine,
122-
}[self.dtype.type]
124+
return self._engine_types[self.dtype]
123125

124126
@cache_readonly
125127
def inferred_type(self) -> str:

pandas/tests/api/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def test_api(self):
214214
+ self.funcs_to
215215
+ self.private_modules
216216
)
217-
self.check(pd, checkthese, self.ignored)
217+
self.check(namespace=pd, expected=checkthese, ignored=self.ignored)
218218

219219
def test_depr(self):
220220
deprecated_list = (

pandas/tests/base/test_unique.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ def test_unique(index_or_series_obj):
2323
if isinstance(obj, pd.MultiIndex):
2424
expected = pd.MultiIndex.from_tuples(unique_values)
2525
expected.names = obj.names
26-
tm.assert_index_equal(result, expected)
26+
tm.assert_index_equal(result, expected, exact=True)
2727
elif isinstance(obj, pd.Index):
2828
expected = pd.Index(unique_values, dtype=obj.dtype)
2929
if is_datetime64tz_dtype(obj.dtype):
3030
expected = expected.normalize()
31-
tm.assert_index_equal(result, expected)
31+
tm.assert_index_equal(result, expected, exact=True)
3232
else:
3333
expected = np.array(unique_values)
3434
tm.assert_numpy_array_equal(result, expected)
@@ -67,7 +67,7 @@ def test_unique_null(null_obj, index_or_series_obj):
6767
if is_datetime64tz_dtype(obj.dtype):
6868
result = result.normalize()
6969
expected = expected.normalize()
70-
tm.assert_index_equal(result, expected)
70+
tm.assert_index_equal(result, expected, exact=True)
7171
else:
7272
expected = np.array(unique_values, dtype=obj.dtype)
7373
tm.assert_numpy_array_equal(result, expected)
@@ -118,7 +118,7 @@ def test_unique_bad_unicode(idx_or_series_w_bad_unicode):
118118

119119
if isinstance(obj, pd.Index):
120120
expected = pd.Index(["\ud83d"], dtype=object)
121-
tm.assert_index_equal(result, expected)
121+
tm.assert_index_equal(result, expected, exact=True)
122122
else:
123123
expected = np.array(["\ud83d"], dtype=object)
124124
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)