Skip to content

Commit a512bbf

Browse files
topper-123JulianWgs
authored andcommitted
REF: Various things preparing instantiable NumericIndex (pandas-dev#41479)
1 parent fdd2917 commit a512bbf

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

pandas/core/indexes/base.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,19 @@ def _outer_indexer(
347347
joined = self._from_join_target(joined_ndarray)
348348
return joined, lidx, ridx
349349

350-
_typ = "index"
350+
_typ: str = "index"
351351
_data: ExtensionArray | np.ndarray
352352
_id: object | None = None
353353
_name: Hashable = None
354354
# MultiIndex.levels previously allowed setting the index name. We
355355
# don't allow this anymore, and raise if it happens rather than
356356
# failing silently.
357357
_no_setting_name: bool = False
358-
_comparables = ["name"]
359-
_attributes = ["name"]
360-
_is_numeric_dtype = False
361-
_can_hold_na = True
362-
_can_hold_strings = True
358+
_comparables: list[str] = ["name"]
359+
_attributes: list[str] = ["name"]
360+
_is_numeric_dtype: bool = False
361+
_can_hold_na: bool = True
362+
_can_hold_strings: bool = True
363363

364364
# would we like our indexing holder to defer to us
365365
_defer_to_indexing = False
@@ -5480,7 +5480,7 @@ def map(self, mapper, na_action=None):
54805480
"""
54815481
from pandas.core.indexes.multi import MultiIndex
54825482

5483-
new_values = super()._map_values(mapper, na_action=na_action)
5483+
new_values = self._map_values(mapper, na_action=na_action)
54845484

54855485
attributes = self._get_attributes_dict()
54865486

pandas/tests/indexes/numeric/test_numeric.py

+27-26
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
class TestFloat64Index(NumericBase):
1919
_index_cls = Float64Index
2020

21-
@pytest.fixture
21+
@pytest.fixture(params=[np.float64])
2222
def dtype(self, request):
23-
return np.float64
23+
return request.param
2424

2525
@pytest.fixture(
26-
params=["int64", "uint64", "category", "datetime64"],
26+
params=["int64", "uint64", "category", "datetime64", "object"],
2727
)
2828
def invalid_dtype(self, request):
2929
return request.param
@@ -42,16 +42,16 @@ def simple_index(self, dtype):
4242
],
4343
ids=["mixed", "float", "mixed_dec", "float_dec"],
4444
)
45-
def index(self, request):
46-
return self._index_cls(request.param)
45+
def index(self, request, dtype):
46+
return self._index_cls(request.param, dtype=dtype)
4747

4848
@pytest.fixture
49-
def mixed_index(self):
50-
return self._index_cls([1.5, 2, 3, 4, 5])
49+
def mixed_index(self, dtype):
50+
return self._index_cls([1.5, 2, 3, 4, 5], dtype=dtype)
5151

5252
@pytest.fixture
53-
def float_index(self):
54-
return self._index_cls([0.0, 2.5, 5.0, 7.5, 10.0])
53+
def float_index(self, dtype):
54+
return self._index_cls([0.0, 2.5, 5.0, 7.5, 10.0], dtype=dtype)
5555

5656
def test_repr_roundtrip(self, index):
5757
tm.assert_index_equal(eval(repr(index)), index)
@@ -72,22 +72,23 @@ def test_constructor(self, dtype):
7272
index_cls = self._index_cls
7373

7474
# explicit construction
75-
index = index_cls([1, 2, 3, 4, 5])
75+
index = index_cls([1, 2, 3, 4, 5], dtype=dtype)
7676

7777
assert isinstance(index, index_cls)
78-
assert index.dtype.type is dtype
78+
assert index.dtype == dtype
7979

8080
expected = np.array([1, 2, 3, 4, 5], dtype=dtype)
8181
tm.assert_numpy_array_equal(index.values, expected)
82-
index = index_cls(np.array([1, 2, 3, 4, 5]))
82+
83+
index = index_cls(np.array([1, 2, 3, 4, 5]), dtype=dtype)
8384
assert isinstance(index, index_cls)
8485
assert index.dtype == dtype
8586

86-
index = index_cls([1.0, 2, 3, 4, 5])
87+
index = index_cls([1.0, 2, 3, 4, 5], dtype=dtype)
8788
assert isinstance(index, index_cls)
8889
assert index.dtype == dtype
8990

90-
index = index_cls(np.array([1.0, 2, 3, 4, 5]))
91+
index = index_cls(np.array([1.0, 2, 3, 4, 5]), dtype=dtype)
9192
assert isinstance(index, index_cls)
9293
assert index.dtype == dtype
9394

@@ -100,13 +101,13 @@ def test_constructor(self, dtype):
100101
assert index.dtype == dtype
101102

102103
# nan handling
103-
result = index_cls([np.nan, np.nan])
104+
result = index_cls([np.nan, np.nan], dtype=dtype)
104105
assert pd.isna(result.values).all()
105106

106-
result = index_cls(np.array([np.nan]))
107+
result = index_cls(np.array([np.nan]), dtype=dtype)
107108
assert pd.isna(result.values).all()
108109

109-
result = Index(np.array([np.nan]))
110+
result = Index(np.array([np.nan], dtype=dtype))
110111
assert isinstance(result, index_cls)
111112
assert result.dtype == dtype
112113
assert pd.isna(result.values).all()
@@ -281,7 +282,7 @@ class NumericInt(NumericBase):
281282
def test_view(self, dtype):
282283
index_cls = self._index_cls
283284

284-
idx = index_cls([], name="Foo")
285+
idx = index_cls([], dtype=dtype, name="Foo")
285286
idx_view = idx.view()
286287
assert idx_view.name == "Foo"
287288

@@ -382,12 +383,12 @@ def test_prevent_casting(self, simple_index):
382383
class TestInt64Index(NumericInt):
383384
_index_cls = Int64Index
384385

385-
@pytest.fixture
386-
def dtype(self):
387-
return np.int64
386+
@pytest.fixture(params=[np.int64])
387+
def dtype(self, request):
388+
return request.param
388389

389390
@pytest.fixture(
390-
params=["uint64", "float64", "category", "datetime64"],
391+
params=["uint64", "float64", "category", "datetime64", "object"],
391392
)
392393
def invalid_dtype(self, request):
393394
return request.param
@@ -399,14 +400,14 @@ def simple_index(self, dtype):
399400
@pytest.fixture(
400401
params=[range(0, 20, 2), range(19, -1, -1)], ids=["index_inc", "index_dec"]
401402
)
402-
def index(self, request):
403-
return self._index_cls(request.param)
403+
def index(self, request, dtype):
404+
return self._index_cls(request.param, dtype=dtype)
404405

405406
def test_constructor(self, dtype):
406407
index_cls = self._index_cls
407408

408409
# pass list, coerce fine
409-
index = index_cls([-5, 0, 1, 2])
410+
index = index_cls([-5, 0, 1, 2], dtype=dtype)
410411
expected = Index([-5, 0, 1, 2], dtype=dtype)
411412
tm.assert_index_equal(index, expected)
412413

@@ -486,7 +487,7 @@ def dtype(self):
486487
return np.uint64
487488

488489
@pytest.fixture(
489-
params=["int64", "float64", "category", "datetime64"],
490+
params=["int64", "float64", "category", "datetime64", "object"],
490491
)
491492
def invalid_dtype(self, request):
492493
return request.param

pandas/tests/indexes/ranges/test_range.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def dtype(self):
2828
return np.int64
2929

3030
@pytest.fixture(
31-
params=["uint64", "float64", "category", "datetime64"],
31+
params=["uint64", "float64", "category", "datetime64", "object"],
3232
)
3333
def invalid_dtype(self, request):
3434
return request.param

0 commit comments

Comments
 (0)