Skip to content
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

CLN: de-duplicate index validation code #22329

Merged
merged 6 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 1 addition & 17 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,7 @@ cpdef get_value_at(ndarray arr, object loc, object tz=None):


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 = <Py_ssize_t> 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')

return get_value_at(arr, i, tz=None)
return get_value_at(arr, loc, tz=None)


# Don't populate hash tables in monotonic indexes larger than this
Expand Down
55 changes: 36 additions & 19 deletions pandas/_libs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,50 @@ ctypedef fused numeric:
cnp.float64_t


cdef inline object get_value_at(ndarray arr, object loc):
cdef inline Py_ssize_t validate_indexer(ndarray arr, object loc) except -1:
"""
Cast the given indexer `loc` to an integer. If it is negative, i.e. a
python-style indexing-from-the-end indexer, translate it to a
from-the-front indexer. Raise if this is not possible.

Parameters
----------
arr : ndarray
loc : object

Returns
-------
idx : Py_ssize_t

Raises
------
IndexError
"""
cdef:
Py_ssize_t i, sz
Py_ssize_t idx, size
int casted

if is_float_object(loc):
casted = int(loc)
if casted == loc:
loc = casted
i = <Py_ssize_t> loc
sz = cnp.PyArray_SIZE(arr)

if i < 0 and sz > 0:
i += sz
elif i >= sz or sz == 0:
idx = <Py_ssize_t>loc
size = cnp.PyArray_SIZE(arr)

if idx < 0 and size > 0:
idx += size
if idx >= size or size == 0 or idx < 0:
raise IndexError('index out of bounds')

return idx


cdef inline object get_value_at(ndarray arr, object loc):
cdef:
Py_ssize_t i

i = validate_indexer(arr, loc)
return get_value_1d(arr, i)


Expand All @@ -71,19 +98,9 @@ cdef inline set_value_at_unsafe(ndarray arr, object loc, object value):
flag above the loop and then eschew the check on each iteration.
"""
cdef:
Py_ssize_t i, sz
if is_float_object(loc):
casted = int(loc)
if casted == loc:
loc = casted
i = <Py_ssize_t> loc
sz = cnp.PyArray_SIZE(arr)

if i < 0:
i += sz
elif i >= sz:
raise IndexError('index out of bounds')
Py_ssize_t i

i = validate_indexer(arr, loc)
assign_value_1d(arr, i, value)


Expand Down