Skip to content

Commit 45b6f75

Browse files
committed
Easy parts of pandas-dev#23368
1 parent 7a9adb7 commit 45b6f75

File tree

6 files changed

+37
-28
lines changed

6 files changed

+37
-28
lines changed

pandas/_libs/hashtable_func_helper.pxi.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ def duplicated_{{dtype}}({{scalar}}[:] values, object keep='first'):
151151

152152
if keep == 'last':
153153
{{if dtype == 'object'}}
154-
for i in range(n):
154+
for i from n > i >= 0:
155155
kh_put_{{ttype}}(table, <PyObject*>values[i], &ret)
156156
out[i] = ret == 0
157157
{{else}}
158158
with nogil:
159-
for i in range(n):
159+
for i from n > i >= 0:
160160
kh_put_{{ttype}}(table, values[i], &ret)
161161
out[i] = ret == 0
162162
{{endif}}

pandas/_libs/tslibs/ccalendar.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ weekday_to_int = {int_to_weekday[key]: key for key in int_to_weekday}
5454

5555
@cython.wraparound(False)
5656
@cython.boundscheck(False)
57-
cpdef inline int32_t get_days_in_month(int year, Py_ssize_t month) nogil:
57+
cpdef int32_t get_days_in_month(int year, Py_ssize_t month) nogil:
5858
"""Return the number of days in the given month of the given year.
5959
6060
Parameters

pandas/_libs/tslibs/conversion.pyx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1:
7575

7676
@cython.boundscheck(False)
7777
@cython.wraparound(False)
78-
def ensure_datetime64ns(ndarray arr, copy=True):
78+
def ensure_datetime64ns(arr: ndarray, copy: bool=True):
7979
"""
8080
Ensure a np.datetime64 array has dtype specifically 'datetime64[ns]'
8181
@@ -122,7 +122,7 @@ def ensure_datetime64ns(ndarray arr, copy=True):
122122
return result
123123

124124

125-
def ensure_timedelta64ns(ndarray arr, copy=True):
125+
def ensure_timedelta64ns(arr: ndarray, copy: bool=True):
126126
"""
127127
Ensure a np.timedelta64 array has dtype specifically 'timedelta64[ns]'
128128
@@ -137,11 +137,12 @@ def ensure_timedelta64ns(ndarray arr, copy=True):
137137
138138
"""
139139
return arr.astype(TD_DTYPE, copy=copy)
140+
# TODO: check for overflows when going from a lower-resolution to nanos
140141

141142

142143
@cython.boundscheck(False)
143144
@cython.wraparound(False)
144-
def datetime_to_datetime64(object[:] values):
145+
def datetime_to_datetime64(values: object[:]):
145146
"""
146147
Convert ndarray of datetime-like objects to int64 array representing
147148
nanosecond timestamps.
@@ -614,6 +615,8 @@ cpdef inline datetime localize_pydatetime(datetime dt, object tz):
614615
# ----------------------------------------------------------------------
615616
# Timezone Conversion
616617

618+
@cython.boundscheck(False)
619+
@cython.wraparound(False)
617620
cdef inline int64_t[:] _tz_convert_dst(int64_t[:] values, tzinfo tz,
618621
bint to_utc=True):
619622
"""
@@ -1220,7 +1223,7 @@ cdef inline int64_t _normalized_stamp(npy_datetimestruct *dts) nogil:
12201223

12211224
@cython.wraparound(False)
12221225
@cython.boundscheck(False)
1223-
def is_date_array_normalized(int64_t[:] stamps, tz=None):
1226+
def is_date_array_normalized(int64_t[:] stamps, object tz=None):
12241227
"""
12251228
Check if all of the given (nanosecond) timestamps are normalized to
12261229
midnight, i.e. hour == minute == second == 0. If the optional timezone

pandas/_libs/tslibs/fields.pyx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def get_date_name_field(int64_t[:] dtindex, object field, object locale=None):
114114
dt64_to_dtstruct(dtindex[i], &dts)
115115
dow = dayofweek(dts.year, dts.month, dts.day)
116116
out[i] = names[dow].capitalize()
117-
return out
117+
118118
elif field == 'month_name':
119119
if locale is None:
120120
names = np.array(MONTHS_FULL, dtype=np.object_)
@@ -128,12 +128,15 @@ def get_date_name_field(int64_t[:] dtindex, object field, object locale=None):
128128

129129
dt64_to_dtstruct(dtindex[i], &dts)
130130
out[i] = names[dts.month].capitalize()
131-
return out
132131

133-
raise ValueError("Field %s not supported" % field)
132+
else:
133+
raise ValueError("Field {field} not supported".format(field=field))
134+
135+
return out
134136

135137

136138
@cython.wraparound(False)
139+
@cython.boundscheck(False)
137140
def get_start_end_field(int64_t[:] dtindex, object field,
138141
object freqstr=None, int month_kw=12):
139142
"""
@@ -163,8 +166,8 @@ def get_start_end_field(int64_t[:] dtindex, object field,
163166

164167
if freqstr:
165168
if freqstr == 'C':
166-
raise ValueError(
167-
"Custom business days is not supported by %s" % field)
169+
raise ValueError("Custom business days is not supported by {field}"
170+
.format(field=field))
168171
is_business = freqstr[0] == 'B'
169172

170173
# YearBegin(), BYearBegin() use month = starting month of year.
@@ -196,7 +199,7 @@ def get_start_end_field(int64_t[:] dtindex, object field,
196199

197200
if (dom == 1 and dow < 5) or (dom <= 3 and dow == 0):
198201
out[i] = 1
199-
return out.view(bool)
202+
200203
else:
201204
for i in range(count):
202205
if dtindex[i] == NPY_NAT:
@@ -208,7 +211,6 @@ def get_start_end_field(int64_t[:] dtindex, object field,
208211

209212
if dom == 1:
210213
out[i] = 1
211-
return out.view(bool)
212214

213215
elif field == 'is_month_end':
214216
if is_business:
@@ -228,7 +230,7 @@ def get_start_end_field(int64_t[:] dtindex, object field,
228230
if (ldom == doy and dow < 5) or (
229231
dow == 4 and (ldom - doy <= 2)):
230232
out[i] = 1
231-
return out.view(bool)
233+
232234
else:
233235
for i in range(count):
234236
if dtindex[i] == NPY_NAT:
@@ -244,7 +246,6 @@ def get_start_end_field(int64_t[:] dtindex, object field,
244246

245247
if ldom == doy:
246248
out[i] = 1
247-
return out.view(bool)
248249

249250
elif field == 'is_quarter_start':
250251
if is_business:
@@ -260,7 +261,7 @@ def get_start_end_field(int64_t[:] dtindex, object field,
260261
if ((dts.month - start_month) % 3 == 0) and (
261262
(dom == 1 and dow < 5) or (dom <= 3 and dow == 0)):
262263
out[i] = 1
263-
return out.view(bool)
264+
264265
else:
265266
for i in range(count):
266267
if dtindex[i] == NPY_NAT:
@@ -272,7 +273,6 @@ def get_start_end_field(int64_t[:] dtindex, object field,
272273

273274
if ((dts.month - start_month) % 3 == 0) and dom == 1:
274275
out[i] = 1
275-
return out.view(bool)
276276

277277
elif field == 'is_quarter_end':
278278
if is_business:
@@ -293,7 +293,7 @@ def get_start_end_field(int64_t[:] dtindex, object field,
293293
(ldom == doy and dow < 5) or (
294294
dow == 4 and (ldom - doy <= 2))):
295295
out[i] = 1
296-
return out.view(bool)
296+
297297
else:
298298
for i in range(count):
299299
if dtindex[i] == NPY_NAT:
@@ -309,7 +309,6 @@ def get_start_end_field(int64_t[:] dtindex, object field,
309309

310310
if ((dts.month - end_month) % 3 == 0) and (ldom == doy):
311311
out[i] = 1
312-
return out.view(bool)
313312

314313
elif field == 'is_year_start':
315314
if is_business:
@@ -325,7 +324,7 @@ def get_start_end_field(int64_t[:] dtindex, object field,
325324
if (dts.month == start_month) and (
326325
(dom == 1 and dow < 5) or (dom <= 3 and dow == 0)):
327326
out[i] = 1
328-
return out.view(bool)
327+
329328
else:
330329
for i in range(count):
331330
if dtindex[i] == NPY_NAT:
@@ -337,7 +336,6 @@ def get_start_end_field(int64_t[:] dtindex, object field,
337336

338337
if (dts.month == start_month) and dom == 1:
339338
out[i] = 1
340-
return out.view(bool)
341339

342340
elif field == 'is_year_end':
343341
if is_business:
@@ -358,7 +356,7 @@ def get_start_end_field(int64_t[:] dtindex, object field,
358356
(ldom == doy and dow < 5) or (
359357
dow == 4 and (ldom - doy <= 2))):
360358
out[i] = 1
361-
return out.view(bool)
359+
362360
else:
363361
for i in range(count):
364362
if dtindex[i] == NPY_NAT:
@@ -374,9 +372,11 @@ def get_start_end_field(int64_t[:] dtindex, object field,
374372

375373
if (dts.month == end_month) and (ldom == doy):
376374
out[i] = 1
377-
return out.view(bool)
378375

379-
raise ValueError("Field %s not supported" % field)
376+
else:
377+
raise ValueError("Field {field} not supported".format(field=field))
378+
379+
return out.view(bool)
380380

381381

382382
@cython.wraparound(False)

pandas/_libs/tslibs/offsets.pyx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,8 @@ def shift_months(int64_t[:] dtindex, int months, object day=None):
816816
return np.asarray(out)
817817

818818

819-
cpdef datetime shift_month(datetime stamp, int months, object day_opt=None):
819+
def shift_month(stamp: datetime, months: int,
820+
day_opt: object=None) -> datetime:
820821
"""
821822
Given a datetime (or Timestamp) `stamp`, an integer `months` and an
822823
option `day_opt`, return a new datetimelike that many months later,
@@ -946,8 +947,8 @@ cpdef int roll_convention(int other, int n, int compare) nogil:
946947
return n
947948

948949

949-
cpdef int roll_qtrday(datetime other, int n, int month, object day_opt,
950-
int modby=3) except? -1:
950+
def roll_qtrday(other: datetime, n: int, month: int,
951+
day_opt: object, modby: int=3) -> int:
951952
"""
952953
Possibly increment or decrement the number of periods to shift
953954
based on rollforward/rollbackward conventions.

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import warnings
66
import sys
77
cdef bint PY3 = (sys.version_info[0] >= 3)
88

9+
import cython
910
from cython import Py_ssize_t
1011

1112
from cpython cimport Py_NE, Py_EQ, PyObject_RichCompare
@@ -82,6 +83,8 @@ _no_input = object()
8283
# ----------------------------------------------------------------------
8384
# API
8485

86+
@cython.boundscheck(False)
87+
@cython.wraparound(False)
8588
def ints_to_pytimedelta(int64_t[:] arr, box=False):
8689
"""
8790
convert an i8 repr to an ndarray of timedelta or Timedelta (if box ==
@@ -198,6 +201,8 @@ cpdef convert_to_timedelta64(object ts, object unit):
198201
return ts.astype('timedelta64[ns]')
199202

200203

204+
@cython.boundscheck(False)
205+
@cython.wraparound(False)
201206
def array_to_timedelta64(object[:] values, unit='ns', errors='raise'):
202207
"""
203208
Convert an ndarray to an array of timedeltas. If errors == 'coerce',

0 commit comments

Comments
 (0)