From b9195cd7a809b6d28d82e2dd05d1e3900c1b275d Mon Sep 17 00:00:00 2001 From: Terji Petersen Date: Mon, 17 May 2021 16:19:46 +0100 Subject: [PATCH] REF: Various things preparing instantiable NumericIndex (#41479) --- pandas/core/indexes/base.py | 14 +++--- pandas/tests/indexes/numeric/test_numeric.py | 53 ++++++++++---------- pandas/tests/indexes/ranges/test_range.py | 2 +- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index a366b49ce3c55..9f0a80ba0f5c7 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -347,7 +347,7 @@ 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 @@ -355,11 +355,11 @@ def _outer_indexer( # 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 @@ -5480,7 +5480,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() diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index 4c2c38df601ce..c796a25faf0a6 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -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 @@ -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) @@ -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 @@ -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() @@ -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" @@ -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 @@ -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) @@ -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 diff --git a/pandas/tests/indexes/ranges/test_range.py b/pandas/tests/indexes/ranges/test_range.py index d9b093cc97fda..e80868fb08a09 100644 --- a/pandas/tests/indexes/ranges/test_range.py +++ b/pandas/tests/indexes/ranges/test_range.py @@ -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