Skip to content

Commit a740748

Browse files
authored
CLN: Move stuff in tests.indexes.test_numeric.py to more logical locations (pandas-dev#41254)
1 parent 601c43c commit a740748

File tree

3 files changed

+99
-105
lines changed

3 files changed

+99
-105
lines changed

pandas/tests/indexes/common.py

+94-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from datetime import datetime
12
import gc
23
from typing import Type
34

45
import numpy as np
56
import pytest
67

78
from pandas._libs import iNaT
9+
from pandas._libs.tslibs import Timestamp
810

911
from pandas.core.dtypes.common import is_datetime64tz_dtype
1012
from pandas.core.dtypes.dtypes import CategoricalDtype
@@ -13,6 +15,7 @@
1315
from pandas import (
1416
CategoricalIndex,
1517
DatetimeIndex,
18+
Float64Index,
1619
Index,
1720
Int64Index,
1821
IntervalIndex,
@@ -29,7 +32,9 @@
2932

3033

3134
class Base:
32-
""" base class for index sub-class tests """
35+
"""
36+
Base class for index sub-class tests.
37+
"""
3338

3439
_index_cls: Type[Index]
3540

@@ -738,3 +743,91 @@ def test_shallow_copy_shares_cache(self, simple_index):
738743
shallow_copy = idx._shallow_copy(idx._data)
739744
assert shallow_copy._cache is not idx._cache
740745
assert shallow_copy._cache == {}
746+
747+
def test_index_groupby(self, simple_index):
748+
idx = simple_index[:5]
749+
to_groupby = np.array([1, 2, np.nan, 2, 1])
750+
tm.assert_dict_equal(
751+
idx.groupby(to_groupby), {1.0: idx[[0, 4]], 2.0: idx[[1, 3]]}
752+
)
753+
754+
to_groupby = DatetimeIndex(
755+
[
756+
datetime(2011, 11, 1),
757+
datetime(2011, 12, 1),
758+
pd.NaT,
759+
datetime(2011, 12, 1),
760+
datetime(2011, 11, 1),
761+
],
762+
tz="UTC",
763+
).values
764+
765+
ex_keys = [Timestamp("2011-11-01"), Timestamp("2011-12-01")]
766+
expected = {ex_keys[0]: idx[[0, 4]], ex_keys[1]: idx[[1, 3]]}
767+
tm.assert_dict_equal(idx.groupby(to_groupby), expected)
768+
769+
770+
class NumericBase(Base):
771+
"""
772+
Base class for numeric index (incl. RangeIndex) sub-class tests.
773+
"""
774+
775+
def test_where(self):
776+
# Tested in numeric.test_indexing
777+
pass
778+
779+
def test_can_hold_identifiers(self, simple_index):
780+
idx = simple_index
781+
key = idx[0]
782+
assert idx._can_hold_identifiers_and_holds_name(key) is False
783+
784+
def test_format(self, simple_index):
785+
# GH35439
786+
idx = simple_index
787+
max_width = max(len(str(x)) for x in idx)
788+
expected = [str(x).ljust(max_width) for x in idx]
789+
assert idx.format() == expected
790+
791+
def test_numeric_compat(self):
792+
pass # override Base method
793+
794+
def test_insert_na(self, nulls_fixture, simple_index):
795+
# GH 18295 (test missing)
796+
index = simple_index
797+
na_val = nulls_fixture
798+
799+
if na_val is pd.NaT:
800+
expected = Index([index[0], pd.NaT] + list(index[1:]), dtype=object)
801+
else:
802+
expected = Float64Index([index[0], np.nan] + list(index[1:]))
803+
804+
result = index.insert(1, na_val)
805+
tm.assert_index_equal(result, expected)
806+
807+
def test_arithmetic_explicit_conversions(self):
808+
# GH 8608
809+
# add/sub are overridden explicitly for Float/Int Index
810+
index_cls = self._index_cls
811+
if index_cls is RangeIndex:
812+
idx = RangeIndex(5)
813+
else:
814+
idx = index_cls(np.arange(5, dtype="int64"))
815+
816+
# float conversions
817+
arr = np.arange(5, dtype="int64") * 3.2
818+
expected = Float64Index(arr)
819+
fidx = idx * 3.2
820+
tm.assert_index_equal(fidx, expected)
821+
fidx = 3.2 * idx
822+
tm.assert_index_equal(fidx, expected)
823+
824+
# interops with numpy arrays
825+
expected = Float64Index(arr)
826+
a = np.zeros(5, dtype="float64")
827+
result = fidx - a
828+
tm.assert_index_equal(result, expected)
829+
830+
expected = Float64Index(-arr)
831+
a = np.zeros(5, dtype="float64")
832+
result = a - fidx
833+
tm.assert_index_equal(result, expected)

pandas/tests/indexes/test_numeric.py renamed to pandas/tests/indexes/numeric/test_numeric.py

+3-102
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from datetime import datetime
2-
31
import numpy as np
42
import pytest
53

@@ -15,107 +13,10 @@
1513
UInt64Index,
1614
)
1715
import pandas._testing as tm
18-
from pandas.tests.indexes.common import Base
19-
20-
21-
class TestArithmetic:
22-
@pytest.mark.parametrize(
23-
"klass", [Float64Index, Int64Index, UInt64Index, RangeIndex]
24-
)
25-
def test_arithmetic_explicit_conversions(self, klass):
26-
27-
# GH 8608
28-
# add/sub are overridden explicitly for Float/Int Index
29-
if klass is RangeIndex:
30-
idx = RangeIndex(5)
31-
else:
32-
idx = klass(np.arange(5, dtype="int64"))
33-
34-
# float conversions
35-
arr = np.arange(5, dtype="int64") * 3.2
36-
expected = Float64Index(arr)
37-
fidx = idx * 3.2
38-
tm.assert_index_equal(fidx, expected)
39-
fidx = 3.2 * idx
40-
tm.assert_index_equal(fidx, expected)
41-
42-
# interops with numpy arrays
43-
expected = Float64Index(arr)
44-
a = np.zeros(5, dtype="float64")
45-
result = fidx - a
46-
tm.assert_index_equal(result, expected)
47-
48-
expected = Float64Index(-arr)
49-
a = np.zeros(5, dtype="float64")
50-
result = a - fidx
51-
tm.assert_index_equal(result, expected)
52-
53-
54-
class TestNumericIndex:
55-
def test_index_groupby(self):
56-
int_idx = Index(range(6))
57-
float_idx = Index(np.arange(0, 0.6, 0.1))
58-
obj_idx = Index("A B C D E F".split())
59-
dt_idx = pd.date_range("2013-01-01", freq="M", periods=6)
60-
61-
for idx in [int_idx, float_idx, obj_idx, dt_idx]:
62-
to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
63-
tm.assert_dict_equal(
64-
idx.groupby(to_groupby), {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]}
65-
)
66-
67-
to_groupby = pd.DatetimeIndex(
68-
[
69-
datetime(2011, 11, 1),
70-
datetime(2011, 12, 1),
71-
pd.NaT,
72-
pd.NaT,
73-
datetime(2011, 12, 1),
74-
datetime(2011, 11, 1),
75-
],
76-
tz="UTC",
77-
).values
78-
79-
ex_keys = [Timestamp("2011-11-01"), Timestamp("2011-12-01")]
80-
expected = {ex_keys[0]: idx[[0, 5]], ex_keys[1]: idx[[1, 4]]}
81-
tm.assert_dict_equal(idx.groupby(to_groupby), expected)
82-
83-
84-
class Numeric(Base):
85-
def test_where(self):
86-
# Tested in numeric.test_indexing
87-
pass
88-
89-
def test_can_hold_identifiers(self, simple_index):
90-
idx = simple_index
91-
key = idx[0]
92-
assert idx._can_hold_identifiers_and_holds_name(key) is False
93-
94-
def test_format(self, simple_index):
95-
# GH35439
96-
idx = simple_index
97-
max_width = max(len(str(x)) for x in idx)
98-
expected = [str(x).ljust(max_width) for x in idx]
99-
assert idx.format() == expected
100-
101-
def test_numeric_compat(self):
102-
pass # override Base method
103-
104-
def test_insert_na(self, nulls_fixture, simple_index):
105-
# GH 18295 (test missing)
106-
index = simple_index
107-
na_val = nulls_fixture
108-
109-
if na_val is pd.NaT:
110-
expected = Index([index[0], pd.NaT] + list(index[1:]), dtype=object)
111-
else:
112-
expected = Float64Index([index[0], np.nan] + list(index[1:]))
113-
114-
result = index.insert(1, na_val)
115-
tm.assert_index_equal(result, expected)
16+
from pandas.tests.indexes.common import NumericBase
11617

11718

118-
class TestFloat64Index(Numeric):
19+
class TestFloat64Index(NumericBase):
11920
_index_cls = Float64Index
12021
_dtype = np.float64
12122

@@ -387,7 +288,7 @@ def test_fillna_float64(self):
387288
tm.assert_index_equal(idx.fillna("obj"), exp)
388289

389290

390-
class NumericInt(Numeric):
291+
class NumericInt(NumericBase):
391292
def test_view(self):
392293
index_cls = self._index_cls
393294

pandas/tests/indexes/ranges/test_range.py

+2-2
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)