diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx index d74673b17c8d9..f20a665578e00 100644 --- a/pandas/_libs/index.pyx +++ b/pandas/_libs/index.pyx @@ -47,6 +47,31 @@ def get_value_at(ndarray arr, object loc): return util.get_value_at(arr, loc) +cpdef object get_value_box(ndarray arr, object loc): + cdef: + Py_ssize_t i, sz + + if util.is_float_object(loc): + casted = int(loc) + if casted == loc: + loc = casted + i = loc + sz = cnp.PyArray_SIZE(arr) + + if i < 0 and sz > 0: + i += sz + + if i >= sz or sz == 0 or i < 0: + raise IndexError('index out of bounds') + + if arr.descr.type_num == NPY_DATETIME: + return Timestamp(util.get_value_1d(arr, i)) + elif arr.descr.type_num == NPY_TIMEDELTA: + return Timedelta(util.get_value_1d(arr, i)) + else: + return util.get_value_1d(arr, i) + + def set_value_at(ndarray arr, object loc, object val): return util.set_value_at(arr, loc, val) diff --git a/pandas/_libs/src/util.pxd b/pandas/_libs/src/util.pxd index 84d6dddf338a5..7361aa36144c5 100644 --- a/pandas/_libs/src/util.pxd +++ b/pandas/_libs/src/util.pxd @@ -53,7 +53,8 @@ cdef extern from "headers/stdint.h": cdef inline object get_value_at(ndarray arr, object loc): cdef: Py_ssize_t i, sz - void* data_ptr + int casted + if is_float_object(loc): casted = int(loc) if casted == loc: diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index e43fddad413a5..65e074625cad7 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -6,7 +6,7 @@ cimport numpy as np from numpy cimport (int32_t, int64_t, import_array, ndarray, - float64_t, NPY_DATETIME, NPY_TIMEDELTA) + float64_t) import numpy as np import sys @@ -848,31 +848,6 @@ cdef inline bint _check_all_nulls(object val): return res -cpdef object get_value_box(ndarray arr, object loc): - cdef: - Py_ssize_t i, sz - - if is_float_object(loc): - casted = int(loc) - if casted == loc: - loc = casted - i = loc - sz = np.PyArray_SIZE(arr) - - if i < 0 and sz > 0: - i += sz - - if i >= sz or sz == 0 or i < 0: - raise IndexError('index out of bounds') - - if arr.descr.type_num == NPY_DATETIME: - return Timestamp(util.get_value_1d(arr, i)) - elif arr.descr.type_num == NPY_TIMEDELTA: - return Timedelta(util.get_value_1d(arr, i)) - else: - return util.get_value_1d(arr, i) - - # Add the min and max fields at the class level cdef int64_t _NS_UPPER_BOUND = INT64_MAX # the smallest value we could actually represent is diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 121bf6a66dee5..36fe6d55f71eb 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -2559,7 +2559,7 @@ def get_value(self, series, key): raise try: - return libts.get_value_box(s, key) + return libindex.get_value_box(s, key) except IndexError: raise except TypeError: