Skip to content

Commit 7d44e3f

Browse files
committed
CLN: move test_arithmetic_explicit_conversions to common.py::NumericBase
1 parent 78b9a0e commit 7d44e3f

File tree

3 files changed

+70
-72
lines changed

3 files changed

+70
-72
lines changed

pandas/tests/indexes/common.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas import (
1616
CategoricalIndex,
1717
DatetimeIndex,
18+
Float64Index,
1819
Index,
1920
Int64Index,
2021
IntervalIndex,
@@ -762,3 +763,67 @@ def test_index_groupby(self, simple_index):
762763
ex_keys = [Timestamp("2011-11-01"), Timestamp("2011-12-01")]
763764
expected = {ex_keys[0]: idx[[0, 4]], ex_keys[1]: idx[[1, 3]]}
764765
tm.assert_dict_equal(idx.groupby(to_groupby), expected)
766+
767+
768+
class NumericBase(Base):
769+
""" base class for numeric index (incl. RangeIndex) sub-class tests """
770+
771+
def test_where(self):
772+
# Tested in numeric.test_indexing
773+
pass
774+
775+
def test_can_hold_identifiers(self, simple_index):
776+
idx = simple_index
777+
key = idx[0]
778+
assert idx._can_hold_identifiers_and_holds_name(key) is False
779+
780+
def test_format(self, simple_index):
781+
# GH35439
782+
idx = simple_index
783+
max_width = max(len(str(x)) for x in idx)
784+
expected = [str(x).ljust(max_width) for x in idx]
785+
assert idx.format() == expected
786+
787+
def test_numeric_compat(self):
788+
pass # override Base method
789+
790+
def test_insert_na(self, nulls_fixture, simple_index):
791+
# GH 18295 (test missing)
792+
index = simple_index
793+
na_val = nulls_fixture
794+
795+
if na_val is pd.NaT:
796+
expected = Index([index[0], pd.NaT] + list(index[1:]), dtype=object)
797+
else:
798+
expected = Float64Index([index[0], np.nan] + list(index[1:]))
799+
800+
result = index.insert(1, na_val)
801+
tm.assert_index_equal(result, expected)
802+
803+
def test_arithmetic_explicit_conversions(self):
804+
# GH 8608
805+
# add/sub are overridden explicitly for Float/Int Index
806+
index_cls = self._index_cls
807+
if index_cls is RangeIndex:
808+
idx = RangeIndex(5)
809+
else:
810+
idx = index_cls(np.arange(5, dtype="int64"))
811+
812+
# float conversions
813+
arr = np.arange(5, dtype="int64") * 3.2
814+
expected = Float64Index(arr)
815+
fidx = idx * 3.2
816+
tm.assert_index_equal(fidx, expected)
817+
fidx = 3.2 * idx
818+
tm.assert_index_equal(fidx, expected)
819+
820+
# interops with numpy arrays
821+
expected = Float64Index(arr)
822+
a = np.zeros(5, dtype="float64")
823+
result = fidx - a
824+
tm.assert_index_equal(result, expected)
825+
826+
expected = Float64Index(-arr)
827+
a = np.zeros(5, dtype="float64")
828+
result = a - fidx
829+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/numeric/test_numeric.py

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -13,77 +13,10 @@
1313
UInt64Index,
1414
)
1515
import pandas._testing as tm
16-
from pandas.tests.indexes.common import Base
16+
from pandas.tests.indexes.common import NumericBase
1717

1818

19-
class TestArithmetic:
20-
@pytest.mark.parametrize(
21-
"klass", [Float64Index, Int64Index, UInt64Index, RangeIndex]
22-
)
23-
def test_arithmetic_explicit_conversions(self, klass):
24-
25-
# GH 8608
26-
# add/sub are overridden explicitly for Float/Int Index
27-
if klass is RangeIndex:
28-
idx = RangeIndex(5)
29-
else:
30-
idx = klass(np.arange(5, dtype="int64"))
31-
32-
# float conversions
33-
arr = np.arange(5, dtype="int64") * 3.2
34-
expected = Float64Index(arr)
35-
fidx = idx * 3.2
36-
tm.assert_index_equal(fidx, expected)
37-
fidx = 3.2 * idx
38-
tm.assert_index_equal(fidx, expected)
39-
40-
# interops with numpy arrays
41-
expected = Float64Index(arr)
42-
a = np.zeros(5, dtype="float64")
43-
result = fidx - a
44-
tm.assert_index_equal(result, expected)
45-
46-
expected = Float64Index(-arr)
47-
a = np.zeros(5, dtype="float64")
48-
result = a - fidx
49-
tm.assert_index_equal(result, expected)
50-
51-
52-
class Numeric(Base):
53-
def test_where(self):
54-
# Tested in numeric.test_indexing
55-
pass
56-
57-
def test_can_hold_identifiers(self, simple_index):
58-
idx = simple_index
59-
key = idx[0]
60-
assert idx._can_hold_identifiers_and_holds_name(key) is False
61-
62-
def test_format(self, simple_index):
63-
# GH35439
64-
idx = simple_index
65-
max_width = max(len(str(x)) for x in idx)
66-
expected = [str(x).ljust(max_width) for x in idx]
67-
assert idx.format() == expected
68-
69-
def test_numeric_compat(self):
70-
pass # override Base method
71-
72-
def test_insert_na(self, nulls_fixture, simple_index):
73-
# GH 18295 (test missing)
74-
index = simple_index
75-
na_val = nulls_fixture
76-
77-
if na_val is pd.NaT:
78-
expected = Index([index[0], pd.NaT] + list(index[1:]), dtype=object)
79-
else:
80-
expected = Float64Index([index[0], np.nan] + list(index[1:]))
81-
82-
result = index.insert(1, na_val)
83-
tm.assert_index_equal(result, expected)
84-
85-
86-
class TestFloat64Index(Numeric):
19+
class TestFloat64Index(NumericBase):
8720
_index_cls = Float64Index
8821
_dtype = np.float64
8922

@@ -353,7 +286,7 @@ def test_fillna_float64(self):
353286
tm.assert_index_equal(idx.fillna("obj"), exp)
354287

355288

356-
class NumericInt(Numeric):
289+
class NumericInt(NumericBase):
357290
def test_view(self):
358291
index_cls = self._index_cls
359292

pandas/tests/indexes/ranges/test_range.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RangeIndex,
1212
)
1313
import pandas._testing as tm
14-
from pandas.tests.indexes.test_numeric import Numeric
14+
from pandas.tests.indexes.common import NumericBase
1515

1616
# aliases to make some tests easier to read
1717
RI = RangeIndex
@@ -20,7 +20,7 @@
2020
OI = Index
2121

2222

23-
class TestRangeIndex(Numeric):
23+
class TestRangeIndex(NumericBase):
2424
_index_cls = RangeIndex
2525

2626
@pytest.fixture

0 commit comments

Comments
 (0)