Skip to content

Commit 5a89a51

Browse files
committed
move int8/16/32 type conversions to index_class_helper.pxi.in
1 parent 4d0612e commit 5a89a51

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

pandas/_libs/index.pyx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import cython
55

66
import numpy as np
77
cimport numpy as cnp
8-
from numpy cimport (ndarray, float64_t, int32_t,
9-
int8_t, int16_t, int32_t, int64_t,
10-
uint8_t, uint64_t,
8+
from numpy cimport (ndarray,
9+
float64_t, float32_t,
10+
int64_t,int32_t, int16_t, int8_t,
11+
uint64_t, uint32_t, uint16_t, uint8_t,
1112
intp_t,
1213
# Note: NPY_DATETIME, NPY_TIMEDELTA are only available
1314
# for cimport in cython>=0.27.3
@@ -244,8 +245,6 @@ cdef class IndexEngine:
244245
if not self.is_mapping_populated:
245246

246247
values = self._get_index_values()
247-
if values.dtype in {'int8', 'int16', 'int32'}:
248-
values = algos.ensure_int64(values)
249248
self.mapping = self._make_hash_table(len(values))
250249
self._call_map_locations(values)
251250

pandas/_libs/index_class_helper.pxi.in

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
1212

1313
# name, dtype, ctype
1414
dtypes = [('Float64', 'float64', 'float64_t'),
15+
('Float32', 'float32', 'float32_t'),
1516
('Int64', 'int64', 'int64_t'),
1617
('Int32', 'int32', 'int32_t'),
1718
('Int16', 'int16', 'int16_t'),
1819
('Int8', 'int8', 'int8_t'),
1920
('UInt64', 'uint64', 'uint64_t'),
21+
('UInt32', 'uint32', 'uint32_t'),
22+
('UInt16', 'uint16', 'uint16_t'),
23+
('UInt8', 'uint8', 'uint8_t'),
2024
('Object', 'object', 'object'),
2125
]
2226
}}
@@ -41,11 +45,24 @@ cdef class {{name}}Engine(IndexEngine):
4145
{{if name == 'Object'}}
4246
return _hash.PyObjectHashTable(n)
4347
{{elif name in {'Int8', 'Int16', 'Int32'} }}
48+
# {{name}}HashTable is not available, so we use Int64HashTable
4449
return _hash.Int64HashTable(n)
50+
{{elif name in {'UInt8', 'UInt16', 'UInt32'} }}
51+
# {{name}}HashTable is not available, so we use UInt64HashTable
52+
return _hash.UInt64HashTable(n)
53+
{{elif name in {'Float32'} }}
54+
# {{name}}HashTable is not available, so we use Float64HashTable
55+
return _hash.Float64HashTable(n)
4556
{{else}}
4657
return _hash.{{name}}HashTable(n)
4758
{{endif}}
4859

60+
{{if name in {'Int8', 'Int16', 'Int32'} }}
61+
cpdef _call_map_locations(self, values):
62+
# self.mapping is of type Int64HashTable, so convert dtype of values
63+
self.mapping.map_locations(algos.ensure_int64(values))
64+
{{endif}}
65+
4966
{{if name != 'Float64' and name != 'Object'}}
5067
cdef _check_type(self, object val):
5168
hash(val)

pandas/core/indexes/category.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ class CategoricalIndex(Index, accessor.PandasDelegate):
8686

8787
@property
8888
def _engine_type(self):
89-
type_name = self.codes.dtype.name.capitalize()
90-
return getattr(libindex, "{}Engine".format(type_name))
89+
# self.codes can have dtype int8, int16, int 32 or int64, so we need
90+
# to return the corresponding engine type (libindex.Int8Engine, etc.).
91+
engine_name = "{}Engine".format(self.codes.dtype.name.capitalize())
92+
return getattr(libindex, engine_name)
9193
_attributes = ['name']
9294

9395
def __new__(cls, data=None, categories=None, ordered=None, dtype=None,

0 commit comments

Comments
 (0)