Skip to content

Commit fdba416

Browse files
authored
CLN: remove libindex.get_value_at (#31680)
1 parent beb4859 commit fdba416

File tree

5 files changed

+12
-86
lines changed

5 files changed

+12
-86
lines changed

pandas/_libs/index.pyx

+3-33
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
from datetime import datetime, timedelta, date
21
import warnings
32

4-
import cython
5-
63
import numpy as np
74
cimport numpy as cnp
85
from numpy cimport (ndarray, intp_t,
96
float64_t, float32_t,
107
int64_t, int32_t, int16_t, int8_t,
11-
uint64_t, uint32_t, uint16_t, uint8_t,
12-
# Note: NPY_DATETIME, NPY_TIMEDELTA are only available
13-
# for cimport in cython>=0.27.3
14-
NPY_DATETIME, NPY_TIMEDELTA)
8+
uint64_t, uint32_t, uint16_t, uint8_t
9+
)
1510
cnp.import_array()
1611

1712

@@ -23,7 +18,7 @@ from pandas._libs.tslibs.c_timestamp cimport _Timestamp
2318
from pandas._libs.hashtable cimport HashTable
2419

2520
from pandas._libs import algos, hashtable as _hash
26-
from pandas._libs.tslibs import Timestamp, Timedelta, period as periodlib
21+
from pandas._libs.tslibs import Timedelta, period as periodlib
2722
from pandas._libs.missing import checknull
2823

2924

@@ -35,16 +30,6 @@ cdef inline bint is_definitely_invalid_key(object val):
3530
return False
3631

3732

38-
cpdef get_value_at(ndarray arr, object loc, object tz=None):
39-
obj = util.get_value_at(arr, loc)
40-
41-
if arr.descr.type_num == NPY_DATETIME:
42-
return Timestamp(obj, tz=tz)
43-
elif arr.descr.type_num == NPY_TIMEDELTA:
44-
return Timedelta(obj)
45-
return obj
46-
47-
4833
# Don't populate hash tables in monotonic indexes larger than this
4934
_SIZE_CUTOFF = 1_000_000
5035

@@ -72,21 +57,6 @@ cdef class IndexEngine:
7257
self._ensure_mapping_populated()
7358
return val in self.mapping
7459

75-
cpdef get_value(self, ndarray arr, object key, object tz=None):
76-
"""
77-
Parameters
78-
----------
79-
arr : 1-dimensional ndarray
80-
"""
81-
cdef:
82-
object loc
83-
84-
loc = self.get_loc(key)
85-
if isinstance(loc, slice) or util.is_array(loc):
86-
return arr[loc]
87-
else:
88-
return get_value_at(arr, loc, tz=tz)
89-
9060
cpdef get_loc(self, object val):
9161
cdef:
9262
Py_ssize_t loc

pandas/_libs/util.pxd

-48
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from pandas._libs.tslibs.util cimport *
22

3-
from cython cimport Py_ssize_t
4-
53
cimport numpy as cnp
64
from numpy cimport ndarray
75

@@ -51,49 +49,3 @@ cdef inline void set_array_not_contiguous(ndarray ao) nogil:
5149
PyArray_CLEARFLAGS(ao,
5250
(NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS))
5351

54-
55-
cdef inline Py_ssize_t validate_indexer(ndarray arr, object loc) except -1:
56-
"""
57-
Cast the given indexer `loc` to an integer. If it is negative, i.e. a
58-
python-style indexing-from-the-end indexer, translate it to a
59-
from-the-front indexer. Raise if this is not possible.
60-
61-
Parameters
62-
----------
63-
arr : ndarray
64-
loc : object
65-
66-
Returns
67-
-------
68-
idx : Py_ssize_t
69-
70-
Raises
71-
------
72-
IndexError
73-
"""
74-
cdef:
75-
Py_ssize_t idx, size
76-
int casted
77-
78-
if is_float_object(loc):
79-
casted = int(loc)
80-
if casted == loc:
81-
loc = casted
82-
83-
idx = <Py_ssize_t>loc
84-
size = cnp.PyArray_SIZE(arr)
85-
86-
if idx < 0 and size > 0:
87-
idx += size
88-
if idx >= size or size == 0 or idx < 0:
89-
raise IndexError('index out of bounds')
90-
91-
return idx
92-
93-
94-
cdef inline object get_value_at(ndarray arr, object loc):
95-
cdef:
96-
Py_ssize_t i
97-
98-
i = validate_indexer(arr, loc)
99-
return arr[i]

pandas/core/arrays/sparse/array.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import numpy as np
1111

12-
from pandas._libs import index as libindex, lib
12+
from pandas._libs import lib
1313
import pandas._libs.sparse as splib
1414
from pandas._libs.sparse import BlockIndex, IntIndex, SparseIndex
1515
from pandas._libs.tslibs import NaT
@@ -794,7 +794,9 @@ def _get_val_at(self, loc):
794794
if sp_loc == -1:
795795
return self.fill_value
796796
else:
797-
return libindex.get_value_at(self.sp_values, sp_loc)
797+
val = self.sp_values[sp_loc]
798+
val = com.maybe_box_datetimelike(val, self.sp_values.dtype)
799+
return val
798800

799801
def take(self, indices, allow_fill=False, fill_value=None):
800802
if is_scalar(indices):

pandas/core/common.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ def consensus_name_attr(objs):
7272
return name
7373

7474

75-
def maybe_box_datetimelike(value):
75+
def maybe_box_datetimelike(value, dtype=None):
7676
# turn a datetime like into a Timestamp/timedelta as needed
77+
if dtype == object:
78+
# If we dont have datetime64/timedelta64 dtype, we dont want to
79+
# box datetimelike scalars
80+
return value
7781

7882
if isinstance(value, (np.datetime64, datetime)):
7983
value = tslibs.Timestamp(value)

pandas/tests/indexes/multi/test_get_set.py

-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ def test_get_value_duplicates():
5757
)
5858

5959
assert index.get_loc("D") == slice(0, 3)
60-
with pytest.raises(KeyError, match=r"^'D'$"):
61-
index._engine.get_value(np.array([]), "D")
6260

6361

6462
def test_get_level_values_all_na():

0 commit comments

Comments
 (0)