Skip to content

MAINT: Abstract index helpers in pxi.in file #14989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 4 additions & 125 deletions pandas/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -363,115 +363,6 @@ cdef class IndexEngine:

return result[0:count], missing[0:count_missing]

cdef class Int64Engine(IndexEngine):

cdef _get_index_values(self):
return algos.ensure_int64(self.vgetter())

cdef _make_hash_table(self, n):
return _hash.Int64HashTable(n)

def _call_monotonic(self, values):
return algos.is_monotonic_int64(values, timelike=False)

def get_pad_indexer(self, other, limit=None):
return algos.pad_int64(self._get_index_values(), other,
limit=limit)

def get_backfill_indexer(self, other, limit=None):
return algos.backfill_int64(self._get_index_values(), other,
limit=limit)

cdef _check_type(self, object val):
hash(val)
if util.is_bool_object(val):
raise KeyError(val)
elif util.is_float_object(val):
raise KeyError(val)

cdef _maybe_get_bool_indexer(self, object val):
cdef:
ndarray[uint8_t, cast=True] indexer
ndarray[int64_t] values
int count = 0
Py_ssize_t i, n
int64_t ival
int last_true

if not util.is_integer_object(val):
raise KeyError(val)

ival = val

values = self._get_index_values()
n = len(values)

result = np.empty(n, dtype=bool)
indexer = result.view(np.uint8)

for i in range(n):
if values[i] == val:
count += 1
indexer[i] = 1
last_true = i
else:
indexer[i] = 0

if count == 0:
raise KeyError(val)
if count == 1:
return last_true

return result

cdef class Float64Engine(IndexEngine):

cdef _make_hash_table(self, n):
return _hash.Float64HashTable(n)

cdef _get_index_values(self):
return algos.ensure_float64(self.vgetter())

cdef _maybe_get_bool_indexer(self, object val):
cdef:
ndarray[uint8_t] indexer
ndarray[float64_t] values
int count = 0
Py_ssize_t i, n
int last_true

values = self._get_index_values()
n = len(values)

result = np.empty(n, dtype=bool)
indexer = result.view(np.uint8)

for i in range(n):
if values[i] == val:
count += 1
indexer[i] = 1
last_true = i
else:
indexer[i] = 0

if count == 0:
raise KeyError(val)
if count == 1:
return last_true

return result

def _call_monotonic(self, values):
return algos.is_monotonic_float64(values, timelike=False)

def get_pad_indexer(self, other, limit=None):
return algos.pad_float64(self._get_index_values(), other,
limit=limit)

def get_backfill_indexer(self, other, limit=None):
return algos.backfill_float64(self._get_index_values(), other,
limit=limit)


cdef Py_ssize_t _bin_search(ndarray values, object val) except -1:
cdef:
Expand Down Expand Up @@ -510,22 +401,6 @@ _backfill_functions = {
'float64': algos.backfill_float64
}

cdef class ObjectEngine(IndexEngine):

cdef _make_hash_table(self, n):
return _hash.PyObjectHashTable(n)

def _call_monotonic(self, values):
return algos.is_monotonic_object(values, timelike=False)

def get_pad_indexer(self, other, limit=None):
return algos.pad_object(self._get_index_values(), other,
limit=limit)

def get_backfill_indexer(self, other, limit=None):
return algos.backfill_object(self._get_index_values(), other,
limit=limit)


cdef class DatetimeEngine(Int64Engine):

Expand Down Expand Up @@ -668,3 +543,7 @@ cdef inline _to_i8(object val):

cdef inline bint _is_utc(object tz):
return tz is UTC or isinstance(tz, _du_utc)


# Generated from template.
include "index_class_helper.pxi"
90 changes: 90 additions & 0 deletions pandas/src/index_class_helper.pxi.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""
Template for functions of IndexEngine subclasses.

WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""

#----------------------------------------------------------------------
# IndexEngine Subclass Methods
#----------------------------------------------------------------------

{{py:

# name, dtype, ctype
dtypes = [('Float64', 'float64', 'float64_t'),
('Int64', 'int64', 'int64_t'),
('Object', 'object', 'object')]
}}

{{for name, dtype, ctype in dtypes}}


cdef class {{name}}Engine(IndexEngine):

def _call_monotonic(self, values):
return algos.is_monotonic_{{dtype}}(values, timelike=False)

def get_backfill_indexer(self, other, limit=None):
return algos.backfill_{{dtype}}(self._get_index_values(),
other, limit=limit)

def get_pad_indexer(self, other, limit=None):
return algos.pad_{{dtype}}(self._get_index_values(),
other, limit=limit)

cdef _make_hash_table(self, n):
{{if name == 'Object'}}
return _hash.PyObjectHashTable(n)
{{else}}
return _hash.{{name}}HashTable(n)
{{endif}}

{{if name != 'Float64' and name != 'Object'}}
cdef _check_type(self, object val):
hash(val)
if util.is_bool_object(val):
raise KeyError(val)
elif util.is_float_object(val):
raise KeyError(val)
{{endif}}

{{if name != 'Object'}}
cdef _get_index_values(self):
return algos.ensure_{{dtype}}(self.vgetter())

cdef _maybe_get_bool_indexer(self, object val):
cdef:
ndarray[uint8_t, cast=True] indexer
ndarray[{{ctype}}] values
int count = 0
Py_ssize_t i, n
int last_true

{{if name != 'Float64'}}
if not util.is_integer_object(val):
raise KeyError(val)
{{endif}}

values = self._get_index_values()
n = len(values)

result = np.empty(n, dtype=bool)
indexer = result.view(np.uint8)

for i in range(n):
if values[i] == val:
count += 1
indexer[i] = 1
last_true = i
else:
indexer[i] = 0

if count == 0:
raise KeyError(val)
if count == 1:
return last_true

return result
{{endif}}

{{endfor}}
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def is_platform_mac():
'_join': ['join_helper.pxi.in', 'joins_func_helper.pxi.in'],
'hashtable': ['hashtable_class_helper.pxi.in',
'hashtable_func_helper.pxi.in'],
'index': ['index_class_helper.pxi.in'],
'_sparse': ['sparse_op_helper.pxi.in']
}
_pxifiles = []
Expand Down