Skip to content

Commit 1d29dc4

Browse files
jbrockmendeljreback
authored andcommitted
[CLN] cy cleanup, de-duplication (#21826)
1 parent 1dd05cc commit 1d29dc4

File tree

7 files changed

+15
-30
lines changed

7 files changed

+15
-30
lines changed

pandas/_libs/index.pyx

+4-13
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ cdef inline bint is_definitely_invalid_key(object val):
4343
or PyList_Check(val) or hasattr(val, '_data'))
4444

4545

46-
def get_value_at(ndarray arr, object loc):
46+
cpdef get_value_at(ndarray arr, object loc, object tz=None):
4747
if arr.descr.type_num == NPY_DATETIME:
48-
return Timestamp(util.get_value_at(arr, loc))
48+
return Timestamp(util.get_value_at(arr, loc), tz=tz)
4949
elif arr.descr.type_num == NPY_TIMEDELTA:
5050
return Timedelta(util.get_value_at(arr, loc))
5151
return util.get_value_at(arr, loc)
@@ -68,12 +68,7 @@ cpdef object get_value_box(ndarray arr, object loc):
6868
if i >= sz or sz == 0 or i < 0:
6969
raise IndexError('index out of bounds')
7070

71-
if arr.descr.type_num == NPY_DATETIME:
72-
return Timestamp(util.get_value_1d(arr, i))
73-
elif arr.descr.type_num == NPY_TIMEDELTA:
74-
return Timedelta(util.get_value_1d(arr, i))
75-
else:
76-
return util.get_value_1d(arr, i)
71+
return get_value_at(arr, i, tz=None)
7772

7873

7974
# Don't populate hash tables in monotonic indexes larger than this
@@ -114,11 +109,7 @@ cdef class IndexEngine:
114109
if PySlice_Check(loc) or cnp.PyArray_Check(loc):
115110
return arr[loc]
116111
else:
117-
if arr.descr.type_num == NPY_DATETIME:
118-
return Timestamp(util.get_value_at(arr, loc), tz=tz)
119-
elif arr.descr.type_num == NPY_TIMEDELTA:
120-
return Timedelta(util.get_value_at(arr, loc))
121-
return util.get_value_at(arr, loc)
112+
return get_value_at(arr, loc, tz=tz)
122113

123114
cpdef set_value(self, ndarray arr, object key, object value):
124115
"""

pandas/_libs/indexing.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# cython: profile=False
22

33
cdef class _NDFrameIndexerBase:
4-
'''
4+
"""
55
A base class for _NDFrameIndexer for fast instantiation and attribute
66
access.
7-
'''
7+
"""
88
cdef public object obj, name, _ndim
99

1010
def __init__(self, name, obj):

pandas/_libs/tslibs/ccalendar.pyx

-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ Cython implementations of functions resembling the stdlib calendar module
88
cimport cython
99
from cython cimport Py_ssize_t
1010

11-
cimport numpy as cnp
1211
from numpy cimport int64_t, int32_t
13-
cnp.import_array()
1412

1513
from locale import LC_TIME
1614
from strptime import LocaleTime

pandas/_libs/tslibs/conversion.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ cdef inline void localize_tso(_TSObject obj, tzinfo tz):
556556
elif treat_tz_as_dateutil(tz):
557557
dt64_to_dtstruct(obj.value + deltas[pos], &obj.dts)
558558
else:
559+
# TODO: this case is never reached in the tests, but get_dst_info
560+
# has a path that returns typ = None and empty deltas.
561+
# --> Is this path possible?
559562
pass
560563

561564
obj.tzinfo = tz
@@ -1145,10 +1148,7 @@ cdef ndarray[int64_t] _normalize_local(ndarray[int64_t] stamps, object tz):
11451148
# Adjust datetime64 timestamp, recompute datetimestruct
11461149
trans, deltas, typ = get_dst_info(tz)
11471150

1148-
_pos = trans.searchsorted(stamps, side='right') - 1
1149-
if _pos.dtype != np.int64:
1150-
_pos = _pos.astype(np.int64)
1151-
pos = _pos
1151+
pos = trans.searchsorted(stamps, side='right') - 1
11521152

11531153
# statictzinfo
11541154
if typ not in ['pytz', 'dateutil']:

pandas/_libs/tslibs/period.pyx

+1-4
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
960960
# Adjust datetime64 timestamp, recompute datetimestruct
961961
trans, deltas, typ = get_dst_info(tz)
962962

963-
_pos = trans.searchsorted(stamps, side='right') - 1
964-
if _pos.dtype != np.int64:
965-
_pos = _pos.astype(np.int64)
966-
pos = _pos
963+
pos = trans.searchsorted(stamps, side='right') - 1
967964

968965
# statictzinfo
969966
if typ not in ['pytz', 'dateutil']:

pandas/_libs/tslibs/resolution.pyx

+1-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ cdef _reso_local(ndarray[int64_t] stamps, object tz):
101101
# Adjust datetime64 timestamp, recompute datetimestruct
102102
trans, deltas, typ = get_dst_info(tz)
103103

104-
_pos = trans.searchsorted(stamps, side='right') - 1
105-
if _pos.dtype != np.int64:
106-
_pos = _pos.astype(np.int64)
107-
pos = _pos
104+
pos = trans.searchsorted(stamps, side='right') - 1
108105

109106
# statictzinfo
110107
if typ not in ['pytz', 'dateutil']:

pandas/_libs/tslibs/timezones.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ cdef object get_utc_trans_times_from_dateutil_tz(object tz):
188188
return new_trans
189189

190190

191-
cpdef ndarray unbox_utcoffsets(object transinfo):
191+
cpdef ndarray[int64_t, ndim=1] unbox_utcoffsets(object transinfo):
192192
cdef:
193193
Py_ssize_t i, sz
194194
ndarray[int64_t] arr
@@ -216,6 +216,8 @@ cdef object get_dst_info(object tz):
216216
"""
217217
cache_key = tz_cache_key(tz)
218218
if cache_key is None:
219+
# e.g. pytz.FixedOffset, matplotlib.dates._UTC,
220+
# psycopg2.tz.FixedOffsetTimezone
219221
num = int(get_utcoffset(tz, None).total_seconds()) * 1000000000
220222
return (np.array([NPY_NAT + 1], dtype=np.int64),
221223
np.array([num], dtype=np.int64),

0 commit comments

Comments
 (0)