Skip to content

Commit 9e4738f

Browse files
committed
correct ncategories size + add tests
1 parent 4995a57 commit 9e4738f

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

asv_bench/benchmarks/indexing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import numpy as np
44
import pandas.util.testing as tm
5-
from pandas import (Series, DataFrame, MultiIndex, Int64Index, Float64Index,
5+
from pandas import (Series, DataFrame, MultiIndex,
6+
Int64Index, UInt64Index, Float64Index,
67
IntervalIndex, CategoricalIndex,
78
IndexSlice, concat, date_range)
89
from .pandas_vb_common import setup, Panel # noqa
@@ -11,7 +12,7 @@
1112
class NumericSeriesIndexing(object):
1213

1314
goal_time = 0.2
14-
params = [Int64Index, Float64Index]
15+
params = [Int64Index, UInt64Index, Float64Index]
1516
param = ['index']
1617

1718
def setup(self, index):

pandas/tests/indexes/test_category.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pandas import Categorical, IntervalIndex, compat
1515
from pandas.util.testing import assert_almost_equal
1616
import pandas.core.config as cf
17-
from pandas._libs import index as li
17+
from pandas._libs import index as libindex
1818
import pandas as pd
1919

2020
if PY3:
@@ -1121,18 +1121,18 @@ def test_engine_type(self, nbits):
11211121
"""Check that a CategoricalIndex has the correct engine type.
11221122
"""
11231123
if nbits < 64:
1124-
ncategories = int(2 ** (nbits / 2) / 2) # 128 for nbits==16 etc
1124+
ncategories = int(2 ** (nbits / 2) / 2 + 1) # 129 if nbits==16 etc
11251125
index = CategoricalIndex(range(ncategories))
11261126
else:
11271127
index = CategoricalIndex(['a', 'b', 'c'])
1128-
# having actual 2 ** (64 / 2) / 2 categories is too
1128+
# having actual 2 ** (64 / 2) / 2 + 1 categories is too
11291129
# memory-intensive, so we set codes.dtype manually
11301130
index._values._codes = index._values._codes.astype('int64')
11311131

11321132
dtype = {8: np.int8, 16: np.int16,
11331133
32: np.int32, 64: np.int64}[nbits]
1134-
engine = {8: li.Int8Engine, 16: li.Int16Engine,
1135-
32: li.Int32Engine, 64: li.Int64Engine}[nbits]
1134+
engine = {8: libindex.Int8Engine, 16: libindex.Int16Engine,
1135+
32: libindex.Int32Engine, 64: libindex.Int64Engine}[nbits]
11361136

11371137
assert isinstance(index._engine, engine)
11381138
assert issubclass(index.codes.dtype.type, dtype)

pandas/tests/indexes/test_engine.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import pandas as pd
77
from pandas._libs.index import (Int64Engine, UInt64Engine,
88
Float64Engine, ObjectEngine)
9+
from pandas._libs.lib import is_scalar
10+
import pandas.util.testing as tm
911

1012

1113
class TestNumericEngine(object):
@@ -52,12 +54,39 @@ def test_is_unique(self, values, expected, num_engine):
5254
e = num_engine(lambda: codes, len(codes))
5355
assert e.is_unique is expected
5456

57+
@pytest.mark.parametrize('values, value, expected', [
58+
([1, 2, 3], 2, 1),
59+
([1, 2, 2, 3], 2, slice(1, 3)),
60+
([3, 2, 2, 1], 2, np.array([False, True, True, False])),
61+
([1, 2, 2, 1], 2, np.array([False, True, True, False])),
62+
([1, 3, 2], 2, 2),
63+
])
64+
def test_get_loc(self, values, value, expected, num_engine):
65+
codes = np.array(values, dtype=num_engine._dtype)
66+
e = num_engine(lambda: codes, len(codes))
67+
result = e.get_loc(value)
68+
69+
if isinstance(expected, np.ndarray):
70+
assert (result == expected).all()
71+
else:
72+
assert result == expected
73+
74+
@pytest.mark.parametrize('values, value, error', [
75+
([1, 2, 3], 4, KeyError),
76+
([1, 2, 3], '4', KeyError),
77+
])
78+
def test_get_loc_raises(self, values, value, error, num_engine):
79+
codes = np.array(values, dtype=num_engine._dtype)
80+
e = num_engine(lambda: codes, len(codes))
81+
with pytest.raises(error):
82+
e.get_loc(value)
83+
5584

5685
class TestObjectEngine(object):
5786

5887
def setup_class(cls):
59-
cls.Engine = ObjectEngine
6088
cls.dtype = object
89+
cls.Engine = ObjectEngine
6190

6291
@pytest.mark.parametrize('data', [['a', 'b', 'c']])
6392
def test_engine_type(self, data):
@@ -92,3 +121,30 @@ def test_is_unique(self, values, expected):
92121
codes = np.array(values, dtype=self.dtype)
93122
e = self.Engine(lambda: codes, len(codes))
94123
assert e.is_unique is expected
124+
125+
@pytest.mark.parametrize('values, value, expected', [
126+
(list('abc'), 'b', 1),
127+
(list('abbc'), 'b', slice(1, 3)),
128+
(list('cbba'), 'b', np.array([False, True, True, False])),
129+
(list('abba'), 'b', np.array([False, True, True, False])),
130+
(list('acb'), 'b', 2),
131+
])
132+
def test_get_loc(self, values, value, expected):
133+
codes = np.array(values, dtype=self.dtype)
134+
e = self.Engine(lambda: codes, len(codes))
135+
result = e.get_loc(value)
136+
137+
if isinstance(expected, np.ndarray):
138+
assert (result == expected).all()
139+
else:
140+
assert result == expected
141+
142+
@pytest.mark.parametrize('values, value, error', [
143+
(list('abc'), 'd', KeyError),
144+
(list('abc'), 4, KeyError),
145+
])
146+
def test_get_loc_raises(self, values, value, error):
147+
codes = np.array(values, dtype=self.dtype)
148+
e = self.Engine(lambda: codes, len(codes))
149+
with pytest.raises(error):
150+
e.get_loc(value)

0 commit comments

Comments
 (0)