Skip to content

Commit c65cfb6

Browse files
jbrockmendeljreback
authored andcommitted
remove unnecessary get_value_at calls (#28977)
1 parent 80412cc commit c65cfb6

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

pandas/_libs/index.pyx

+20-10
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ cdef inline bint is_definitely_invalid_key(object val):
4141

4242

4343
cpdef get_value_at(ndarray arr, object loc, object tz=None):
44+
obj = util.get_value_at(arr, loc)
45+
4446
if arr.descr.type_num == NPY_DATETIME:
45-
return Timestamp(util.get_value_at(arr, loc), tz=tz)
47+
return Timestamp(obj, tz=tz)
4648
elif arr.descr.type_num == NPY_TIMEDELTA:
47-
return Timedelta(util.get_value_at(arr, loc))
48-
return util.get_value_at(arr, loc)
49+
return Timedelta(obj)
50+
return obj
4951

5052

5153
# Don't populate hash tables in monotonic indexes larger than this
@@ -102,6 +104,9 @@ cdef class IndexEngine:
102104
arr[loc] = value
103105

104106
cpdef get_loc(self, object val):
107+
cdef:
108+
Py_ssize_t loc
109+
105110
if is_definitely_invalid_key(val):
106111
raise TypeError("'{val}' is an invalid key".format(val=val))
107112

@@ -114,7 +119,7 @@ cdef class IndexEngine:
114119
loc = _bin_search(values, val) # .searchsorted(val, side='left')
115120
if loc >= len(values):
116121
raise KeyError(val)
117-
if util.get_value_at(values, loc) != val:
122+
if values[loc] != val:
118123
raise KeyError(val)
119124
return loc
120125

@@ -352,22 +357,22 @@ cdef Py_ssize_t _bin_search(ndarray values, object val) except -1:
352357
Py_ssize_t mid = 0, lo = 0, hi = len(values) - 1
353358
object pval
354359

355-
if hi == 0 or (hi > 0 and val > util.get_value_at(values, hi)):
360+
if hi == 0 or (hi > 0 and val > values[hi]):
356361
return len(values)
357362

358363
while lo < hi:
359364
mid = (lo + hi) // 2
360-
pval = util.get_value_at(values, mid)
365+
pval = values[mid]
361366
if val < pval:
362367
hi = mid
363368
elif val > pval:
364369
lo = mid + 1
365370
else:
366-
while mid > 0 and val == util.get_value_at(values, mid - 1):
371+
while mid > 0 and val == values[mid - 1]:
367372
mid -= 1
368373
return mid
369374

370-
if val <= util.get_value_at(values, mid):
375+
if val <= values[mid]:
371376
return mid
372377
else:
373378
return mid + 1
@@ -387,13 +392,16 @@ cdef class DatetimeEngine(Int64Engine):
387392
return 'M8[ns]'
388393

389394
def __contains__(self, object val):
395+
cdef:
396+
int64_t loc
397+
390398
if self.over_size_threshold and self.is_monotonic_increasing:
391399
if not self.is_unique:
392400
return self._get_loc_duplicates(val)
393401
values = self._get_index_values()
394402
conv = maybe_datetimelike_to_i8(val)
395403
loc = values.searchsorted(conv, side='left')
396-
return util.get_value_at(values, loc) == conv
404+
return values[loc] == conv
397405

398406
self._ensure_mapping_populated()
399407
return maybe_datetimelike_to_i8(val) in self.mapping
@@ -405,6 +413,8 @@ cdef class DatetimeEngine(Int64Engine):
405413
return algos.is_monotonic(values, timelike=True)
406414

407415
cpdef get_loc(self, object val):
416+
cdef:
417+
int64_t loc
408418
if is_definitely_invalid_key(val):
409419
raise TypeError
410420

@@ -422,7 +432,7 @@ cdef class DatetimeEngine(Int64Engine):
422432
self._date_check_type(val)
423433
raise KeyError(val)
424434

425-
if loc == len(values) or util.get_value_at(values, loc) != conv:
435+
if loc == len(values) or values[loc] != conv:
426436
raise KeyError(val)
427437
return loc
428438

pandas/_libs/lib.pyx

+11-5
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,16 @@ def generate_slices(const int64_t[:] labels, Py_ssize_t ngroups):
782782
return starts, ends
783783

784784

785-
def indices_fast(object index, const int64_t[:] labels, list keys,
785+
def indices_fast(ndarray index, const int64_t[:] labels, list keys,
786786
list sorted_labels):
787+
"""
788+
Parameters
789+
----------
790+
index : ndarray
791+
labels : ndarray[int64]
792+
keys : list
793+
sorted_labels : list[ndarray[int64]]
794+
"""
787795
cdef:
788796
Py_ssize_t i, j, k, lab, cur, start, n = len(labels)
789797
dict result = {}
@@ -803,8 +811,7 @@ def indices_fast(object index, const int64_t[:] labels, list keys,
803811
if lab != -1:
804812
tup = PyTuple_New(k)
805813
for j in range(k):
806-
val = util.get_value_at(keys[j],
807-
sorted_labels[j][i - 1])
814+
val = keys[j][sorted_labels[j][i - 1]]
808815
PyTuple_SET_ITEM(tup, j, val)
809816
Py_INCREF(val)
810817

@@ -814,8 +821,7 @@ def indices_fast(object index, const int64_t[:] labels, list keys,
814821

815822
tup = PyTuple_New(k)
816823
for j in range(k):
817-
val = util.get_value_at(keys[j],
818-
sorted_labels[j][n - 1])
824+
val = keys[j][sorted_labels[j][n - 1]]
819825
PyTuple_SET_ITEM(tup, j, val)
820826
Py_INCREF(val)
821827
result[tup] = index[start:]

pandas/_libs/reduction.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ cdef class Reducer:
121121
for i in range(self.nresults):
122122

123123
if has_ndarray_labels:
124-
name = util.get_value_at(labels, i)
124+
name = labels[i]
125125
elif has_labels:
126126
# labels is an ExtensionArray
127127
name = labels[i]

pandas/core/sorting.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ def get_flattened_iterator(comp_ids, ngroups, levels, labels):
303303

304304

305305
def get_indexer_dict(label_list, keys):
306-
""" return a diction of {labels} -> {indexers} """
307-
shape = list(map(len, keys))
306+
""" return a dict of {labels} -> {indexers} """
307+
shape = [len(x) for x in keys]
308308

309309
group_index = get_group_index(label_list, shape, sort=True, xnull=True)
310310
ngroups = (

0 commit comments

Comments
 (0)