Skip to content

REF: Various things preparing instantiable NumericIndex #41479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,19 @@ def _outer_indexer(
joined = self._from_join_target(joined_ndarray)
return joined, lidx, ridx

_typ = "index"
_typ: str = "index"
_data: ExtensionArray | np.ndarray
_id: object | None = None
_name: Hashable = None
# MultiIndex.levels previously allowed setting the index name. We
# don't allow this anymore, and raise if it happens rather than
# failing silently.
_no_setting_name: bool = False
_comparables = ["name"]
_attributes = ["name"]
_is_numeric_dtype = False
_can_hold_na = True
_can_hold_strings = True
_comparables: list[str] = ["name"]
_attributes: list[str] = ["name"]
_is_numeric_dtype: bool = False
_can_hold_na: bool = True
_can_hold_strings: bool = True

# would we like our indexing holder to defer to us
_defer_to_indexing = False
Expand Down Expand Up @@ -5465,7 +5465,7 @@ def map(self, mapper, na_action=None):
"""
from pandas.core.indexes.multi import MultiIndex

new_values = super()._map_values(mapper, na_action=na_action)
new_values = self._map_values(mapper, na_action=na_action)

attributes = self._get_attributes_dict()

Expand Down
53 changes: 27 additions & 26 deletions pandas/tests/indexes/numeric/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
class TestFloat64Index(NumericBase):
_index_cls = Float64Index

@pytest.fixture
@pytest.fixture(params=[np.float64])
def dtype(self, request):
return np.float64
return request.param

@pytest.fixture(
params=["int64", "uint64", "category", "datetime64"],
params=["int64", "uint64", "category", "datetime64", "object"],
)
def invalid_dtype(self, request):
return request.param
Expand All @@ -42,16 +42,16 @@ def simple_index(self, dtype):
],
ids=["mixed", "float", "mixed_dec", "float_dec"],
)
def index(self, request):
return self._index_cls(request.param)
def index(self, request, dtype):
return self._index_cls(request.param, dtype=dtype)

@pytest.fixture
def mixed_index(self):
return self._index_cls([1.5, 2, 3, 4, 5])
def mixed_index(self, dtype):
return self._index_cls([1.5, 2, 3, 4, 5], dtype=dtype)

@pytest.fixture
def float_index(self):
return self._index_cls([0.0, 2.5, 5.0, 7.5, 10.0])
def float_index(self, dtype):
return self._index_cls([0.0, 2.5, 5.0, 7.5, 10.0], dtype=dtype)

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

# explicit construction
index = index_cls([1, 2, 3, 4, 5])
index = index_cls([1, 2, 3, 4, 5], dtype=dtype)

assert isinstance(index, index_cls)
assert index.dtype.type is dtype
assert index.dtype == dtype

expected = np.array([1, 2, 3, 4, 5], dtype=dtype)
tm.assert_numpy_array_equal(index.values, expected)
index = index_cls(np.array([1, 2, 3, 4, 5]))

index = index_cls(np.array([1, 2, 3, 4, 5]), dtype=dtype)
assert isinstance(index, index_cls)
assert index.dtype == dtype

index = index_cls([1.0, 2, 3, 4, 5])
index = index_cls([1.0, 2, 3, 4, 5], dtype=dtype)
assert isinstance(index, index_cls)
assert index.dtype == dtype

index = index_cls(np.array([1.0, 2, 3, 4, 5]))
index = index_cls(np.array([1.0, 2, 3, 4, 5]), dtype=dtype)
assert isinstance(index, index_cls)
assert index.dtype == dtype

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

# nan handling
result = index_cls([np.nan, np.nan])
result = index_cls([np.nan, np.nan], dtype=dtype)
assert pd.isna(result.values).all()

result = index_cls(np.array([np.nan]))
result = index_cls(np.array([np.nan]), dtype=dtype)
assert pd.isna(result.values).all()

result = Index(np.array([np.nan]))
result = Index(np.array([np.nan], dtype=dtype))
assert isinstance(result, index_cls)
assert result.dtype == dtype
assert pd.isna(result.values).all()
Expand Down Expand Up @@ -281,7 +282,7 @@ class NumericInt(NumericBase):
def test_view(self, dtype):
index_cls = self._index_cls

idx = index_cls([], name="Foo")
idx = index_cls([], dtype=dtype, name="Foo")
idx_view = idx.view()
assert idx_view.name == "Foo"

Expand Down Expand Up @@ -382,12 +383,12 @@ def test_prevent_casting(self, simple_index):
class TestInt64Index(NumericInt):
_index_cls = Int64Index

@pytest.fixture
def dtype(self):
return np.int64
@pytest.fixture(params=[np.int64])
def dtype(self, request):
return request.param

@pytest.fixture(
params=["uint64", "float64", "category", "datetime64"],
params=["uint64", "float64", "category", "datetime64", "object"],
)
def invalid_dtype(self, request):
return request.param
Expand All @@ -399,14 +400,14 @@ def simple_index(self, dtype):
@pytest.fixture(
params=[range(0, 20, 2), range(19, -1, -1)], ids=["index_inc", "index_dec"]
)
def index(self, request):
return self._index_cls(request.param)
def index(self, request, dtype):
return self._index_cls(request.param, dtype=dtype)

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

# pass list, coerce fine
index = index_cls([-5, 0, 1, 2])
index = index_cls([-5, 0, 1, 2], dtype=dtype)
expected = Index([-5, 0, 1, 2], dtype=dtype)
tm.assert_index_equal(index, expected)

Expand Down Expand Up @@ -486,7 +487,7 @@ def dtype(self):
return np.uint64

@pytest.fixture(
params=["int64", "float64", "category", "datetime64"],
params=["int64", "float64", "category", "datetime64", "object"],
)
def invalid_dtype(self, request):
return request.param
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/ranges/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def dtype(self):
return np.int64

@pytest.fixture(
params=["uint64", "float64", "category", "datetime64"],
params=["uint64", "float64", "category", "datetime64", "object"],
)
def invalid_dtype(self, request):
return request.param
Expand Down