Skip to content

Commit c9f1166

Browse files
committed
clean up ASV code a bit
1 parent 4bc74f5 commit c9f1166

File tree

4 files changed

+66
-54
lines changed

4 files changed

+66
-54
lines changed

asv_bench/benchmarks/indexing_engines.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import numpy as np
22

3-
import pandas.util.testing as tm
43
from pandas._libs.index import (Int64Engine, Int32Engine,
54
Int16Engine, Int8Engine,
65
UInt64Engine, UInt32Engine,
@@ -32,10 +31,9 @@ def setup(self, engine, index_type):
3231
}[index_type]
3332

3433
self.data = engine(lambda: array_, len(array_))
35-
self.int_scalar = 2
3634

3735
def time_get_loc(self, engine, index_type):
38-
self.data.get_loc(self.int_scalar)
36+
self.data.get_loc(2)
3937

4038

4139
class ObjectEngineIndexing(object):
@@ -57,7 +55,6 @@ def setup(self, engine, index_type):
5755
}[index_type]
5856

5957
self.data = engine(lambda: array_, len(array_))
60-
self.int_scalar = 'b'
6158

6259
def time_get_loc(self, engine, index_type):
63-
self.data.get_loc(self.int_scalar)
60+
self.data.get_loc(2)

pandas/tests/indexes/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def zero(request):
5454
'UInt64', 'UInt32', 'UInt16', 'UInt8',
5555
'Float64', 'Float32',
5656
])
57-
def num_engine(request):
58-
"""Return the various numeric engines in pd._libs.index
57+
def numeric_indexing_engine(request):
58+
"""Return the various numeric indexing engines in pd._libs.index
5959
"""
6060
return getattr(li, "{}Engine".format(request.param))

pandas/tests/indexes/test_category.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -1122,23 +1122,29 @@ def test_take_invalid_kwargs(self):
11221122

11231123
class TestCategoricalIndexEngine(object):
11241124

1125-
@pytest.mark.parametrize('nbits', [8, 16, 32, 64])
1126-
def test_engine_type(self, nbits):
1127-
"""Check that a CategoricalIndex has the correct engine type.
1125+
def setup_method(self):
1126+
self.n_categories = {np.int8: 1, np.int16: 129, np.int32: 32769}
1127+
1128+
self.engines = {np.int8: libindex.Int8Engine,
1129+
np.int16: libindex.Int16Engine,
1130+
np.int32: libindex.Int32Engine,
1131+
np.int64: libindex.Int64Engine}
1132+
1133+
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.int32, np.int64])
1134+
def test_engine_type(self, dtype):
1135+
"""
1136+
Check that a CategoricalIndex has the correct engine type.
11281137
"""
1129-
if nbits < 64:
1130-
ncategories = int(2 ** (nbits / 2) / 2 + 1) # 129 if nbits==16 etc
1131-
index = CategoricalIndex(range(ncategories))
1138+
if dtype != np.int64:
1139+
n_categories = self.n_categories[dtype]
1140+
index = CategoricalIndex(np.arange(n_categories))
11321141
else:
1133-
index = CategoricalIndex(['a', 'b', 'c'])
1134-
# having actual 2 ** (64 / 2) / 2 + 1 categories is too
1142+
# having actual (2 ** 32) + 1 distinct categories is too
11351143
# memory-intensive, so we set codes.dtype manually
1144+
index = CategoricalIndex(['a', 'b', 'c'])
11361145
index._values._codes = index._values._codes.astype('int64')
11371146

1138-
dtype = {8: np.int8, 16: np.int16,
1139-
32: np.int32, 64: np.int64}[nbits]
1140-
engine = {8: libindex.Int8Engine, 16: libindex.Int16Engine,
1141-
32: libindex.Int32Engine, 64: libindex.Int64Engine}[nbits]
1147+
engine = self.engines[dtype]
11421148

11431149
assert isinstance(index._engine, engine)
1144-
assert issubclass(index.codes.dtype.type, dtype)
1150+
assert index.codes.dtype.type == dtype

pandas/tests/indexes/test_engine.py

+43-34
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
class TestNumericEngine(object):
1212

13-
@pytest.mark.parametrize('data', [[0, 1, 2]])
14-
def test_engine_type(self, data, num_engine):
15-
index = pd.Index(data, dtype=num_engine._dtype)
13+
def setup_class(cls):
14+
cls.data = [1, 2, 3]
15+
16+
def test_engine_type(self, numeric_indexing_engine):
17+
index = pd.Index(self.data, dtype=numeric_indexing_engine._dtype)
1618
if issubclass(index.dtype.type, np.signedinteger):
1719
assert isinstance(index._engine, Int64Engine)
1820
elif issubclass(index.dtype.type, np.unsignedinteger):
@@ -22,34 +24,37 @@ def test_engine_type(self, data, num_engine):
2224
else:
2325
raise TypeError("unexpected dtype {}".format(index.dtype))
2426

25-
@pytest.mark.parametrize('data', [[0, 1, 2]])
26-
def test_is_monotonic_ordered(self, data, num_engine):
27-
codes = np.array(data, dtype=num_engine._dtype)
28-
e = num_engine(lambda: codes, len(codes))
27+
def test_is_monotonic_ordered(self, numeric_indexing_engine):
28+
codes = np.array(self.data, dtype=numeric_indexing_engine._dtype)
29+
e = numeric_indexing_engine(lambda: codes, len(codes))
30+
2931
assert e.is_monotonic_increasing
3032
assert not e.is_monotonic_decreasing
3133

3234
# reverse sort order
33-
codes = np.array(list(reversed(data)), dtype=num_engine._dtype)
34-
e = num_engine(lambda: codes, len(codes))
35+
reversed_data = list(reversed(self.data))
36+
codes = np.array(reversed_data, dtype=numeric_indexing_engine._dtype)
37+
e = numeric_indexing_engine(lambda: codes, len(codes))
38+
3539
assert not e.is_monotonic_increasing
3640
assert e.is_monotonic_decreasing
3741

38-
@pytest.mark.parametrize('data', [[1, 0, 2]])
39-
def test_is_not_monotonic_ordered(self, data, num_engine):
40-
codes = np.array(data, dtype=num_engine._dtype)
41-
e = num_engine(lambda: codes, len(codes))
42+
def test_is_not_monotonic_ordered(self, numeric_indexing_engine):
43+
data = [1, 0, 2]
44+
codes = np.array(data, dtype=numeric_indexing_engine._dtype)
45+
e = numeric_indexing_engine(lambda: codes, len(codes))
46+
4247
assert not e.is_monotonic_increasing
4348
assert not e.is_monotonic_decreasing
4449

4550
@pytest.mark.parametrize('values, expected', [
4651
([1, 2, 3], True),
4752
([1, 1, 2], False),
4853
])
49-
def test_is_unique(self, values, expected, num_engine):
54+
def test_is_unique(self, values, expected, numeric_indexing_engine):
55+
codes = np.array(values, dtype=numeric_indexing_engine._dtype)
56+
e = numeric_indexing_engine(lambda: codes, len(codes))
5057

51-
codes = np.array(values, dtype=num_engine._dtype)
52-
e = num_engine(lambda: codes, len(codes))
5358
assert e.is_unique is expected
5459

5560
@pytest.mark.parametrize('values, value, expected', [
@@ -59,9 +64,9 @@ def test_is_unique(self, values, expected, num_engine):
5964
([1, 2, 2, 1], 2, np.array([False, True, True, False])),
6065
([1, 3, 2], 2, 2),
6166
])
62-
def test_get_loc(self, values, value, expected, num_engine):
63-
codes = np.array(values, dtype=num_engine._dtype)
64-
e = num_engine(lambda: codes, len(codes))
67+
def test_get_loc(self, values, value, expected, numeric_indexing_engine):
68+
codes = np.array(values, dtype=numeric_indexing_engine._dtype)
69+
e = numeric_indexing_engine(lambda: codes, len(codes))
6570
result = e.get_loc(value)
6671

6772
if isinstance(expected, np.ndarray):
@@ -73,51 +78,55 @@ def test_get_loc(self, values, value, expected, num_engine):
7378
([1, 2, 3], 4, KeyError),
7479
([1, 2, 3], '4', KeyError),
7580
])
76-
def test_get_loc_raises(self, values, value, error, num_engine):
77-
codes = np.array(values, dtype=num_engine._dtype)
78-
e = num_engine(lambda: codes, len(codes))
81+
def test_get_loc_raises(self, values, value, error,
82+
numeric_indexing_engine):
83+
codes = np.array(values, dtype=numeric_indexing_engine._dtype)
84+
e = numeric_indexing_engine(lambda: codes, len(codes))
7985
with pytest.raises(error):
8086
e.get_loc(value)
8187

8288

8389
class TestObjectEngine(object):
8490

8591
def setup_class(cls):
92+
cls.data = list('abc')
8693
cls.dtype = object
8794
cls.Engine = ObjectEngine
8895

89-
@pytest.mark.parametrize('data', [['a', 'b', 'c']])
90-
def test_engine_type(self, data):
91-
index = pd.Index(data)
96+
def test_engine_type(self):
97+
index = pd.Index(self.data)
9298
assert isinstance(index._engine, self.Engine)
9399

94-
@pytest.mark.parametrize('data', [['a', 'b', 'c']])
95-
def test_is_monotonic_ordered(self, data):
96-
codes = np.array(data, dtype=self.dtype)
100+
def test_is_monotonic_ordered(self):
101+
codes = np.array(self.data, dtype=self.dtype)
97102
e = self.Engine(lambda: codes, len(codes))
103+
98104
assert e.is_monotonic_increasing
99105
assert not e.is_monotonic_decreasing
100106

101107
# reverse sort order
102-
codes = np.array(list(reversed(data)), dtype=self.dtype)
108+
reversed_data = list(reversed(self.data))
109+
codes = np.array(reversed_data, dtype=self.dtype)
103110
e = self.Engine(lambda: codes, len(codes))
111+
104112
assert not e.is_monotonic_increasing
105113
assert e.is_monotonic_decreasing
106114

107-
@pytest.mark.parametrize('data', [['a', 'c', 'b']])
108-
def test_is_not_monotonic_ordered(self, data):
109-
codes = np.array(data, dtype=self.dtype)
115+
def test_is_not_monotonic_ordered(self):
116+
codes = np.array(list('cab'), dtype=self.dtype)
110117
e = self.Engine(lambda: codes, len(codes))
118+
111119
assert not e.is_monotonic_increasing
112120
assert not e.is_monotonic_decreasing
113121

114122
@pytest.mark.parametrize('values, expected', [
115-
(['a', 'b', 'c'], True),
116-
(['a', 'a', 'b'], False),
123+
(list('abc'), True),
124+
(list('aab'), False),
117125
])
118126
def test_is_unique(self, values, expected):
119127
codes = np.array(values, dtype=self.dtype)
120128
e = self.Engine(lambda: codes, len(codes))
129+
121130
assert e.is_unique is expected
122131

123132
@pytest.mark.parametrize('values, value, expected', [

0 commit comments

Comments
 (0)