Skip to content

Commit 549b0ff

Browse files
authored
CLN: assorted tslibs cleanups, annotations (#35045)
1 parent 6598e39 commit 549b0ff

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

pandas/_libs/tslibs/conversion.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ cdef inline int64_t cast_from_unit(object ts, str unit) except? -1:
7777
return <int64_t>(base * m) + <int64_t>(frac * m)
7878

7979

80-
cpdef inline object precision_from_unit(str unit):
80+
cpdef inline (int64_t, int) precision_from_unit(str unit):
8181
"""
8282
Return a casting of the unit represented to nanoseconds + the precision
8383
to round the fractional part.

pandas/_libs/tslibs/fields.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def build_field_sarray(const int64_t[:] dtindex):
9191

9292
@cython.wraparound(False)
9393
@cython.boundscheck(False)
94-
def get_date_name_field(const int64_t[:] dtindex, object field, object locale=None):
94+
def get_date_name_field(const int64_t[:] dtindex, str field, object locale=None):
9595
"""
9696
Given a int64-based datetime index, return array of strings of date
9797
name based on requested field (e.g. day_name)
@@ -141,7 +141,7 @@ def get_date_name_field(const int64_t[:] dtindex, object field, object locale=No
141141

142142
@cython.wraparound(False)
143143
@cython.boundscheck(False)
144-
def get_start_end_field(const int64_t[:] dtindex, object field,
144+
def get_start_end_field(const int64_t[:] dtindex, str field,
145145
object freqstr=None, int month_kw=12):
146146
"""
147147
Given an int64-based datetime index return array of indicators
@@ -386,7 +386,7 @@ def get_start_end_field(const int64_t[:] dtindex, object field,
386386

387387
@cython.wraparound(False)
388388
@cython.boundscheck(False)
389-
def get_date_field(const int64_t[:] dtindex, object field):
389+
def get_date_field(const int64_t[:] dtindex, str field):
390390
"""
391391
Given a int64-based datetime index, extract the year, month, etc.,
392392
field and return an array of these values.
@@ -548,7 +548,7 @@ def get_date_field(const int64_t[:] dtindex, object field):
548548

549549
@cython.wraparound(False)
550550
@cython.boundscheck(False)
551-
def get_timedelta_field(const int64_t[:] tdindex, object field):
551+
def get_timedelta_field(const int64_t[:] tdindex, str field):
552552
"""
553553
Given a int64-based timedelta index, extract the days, hrs, sec.,
554554
field and return an array of these values.

pandas/_libs/tslibs/nattype.pyx

+10-10
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,23 @@ _nat_scalar_rules[Py_GE] = False
5050
# ----------------------------------------------------------------------
5151

5252

53-
def _make_nan_func(func_name, doc):
53+
def _make_nan_func(func_name: str, doc: str):
5454
def f(*args, **kwargs):
5555
return np.nan
5656
f.__name__ = func_name
5757
f.__doc__ = doc
5858
return f
5959

6060

61-
def _make_nat_func(func_name, doc):
61+
def _make_nat_func(func_name: str, doc: str):
6262
def f(*args, **kwargs):
6363
return c_NaT
6464
f.__name__ = func_name
6565
f.__doc__ = doc
6666
return f
6767

6868

69-
def _make_error_func(func_name, cls):
69+
def _make_error_func(func_name: str, cls):
7070
def f(*args, **kwargs):
7171
raise ValueError(f"NaTType does not support {func_name}")
7272

@@ -282,31 +282,31 @@ cdef class _NaT(datetime):
282282
return NPY_NAT
283283

284284
@property
285-
def is_leap_year(self):
285+
def is_leap_year(self) -> bool:
286286
return False
287287

288288
@property
289-
def is_month_start(self):
289+
def is_month_start(self) -> bool:
290290
return False
291291

292292
@property
293-
def is_quarter_start(self):
293+
def is_quarter_start(self) -> bool:
294294
return False
295295

296296
@property
297-
def is_year_start(self):
297+
def is_year_start(self) -> bool:
298298
return False
299299

300300
@property
301-
def is_month_end(self):
301+
def is_month_end(self) -> bool:
302302
return False
303303

304304
@property
305-
def is_quarter_end(self):
305+
def is_quarter_end(self) -> bool:
306306
return False
307307

308308
@property
309-
def is_year_end(self):
309+
def is_year_end(self) -> bool:
310310
return False
311311

312312

pandas/_libs/tslibs/period.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import cython
1414

1515
from cpython.datetime cimport (
1616
datetime,
17+
tzinfo,
1718
PyDate_Check,
1819
PyDateTime_Check,
1920
PyDateTime_IMPORT,
@@ -1417,7 +1418,7 @@ def extract_freq(ndarray[object] values):
14171418

14181419
@cython.wraparound(False)
14191420
@cython.boundscheck(False)
1420-
def dt64arr_to_periodarr(const int64_t[:] stamps, int freq, object tz):
1421+
def dt64arr_to_periodarr(const int64_t[:] stamps, int freq, tzinfo tz):
14211422
cdef:
14221423
Py_ssize_t n = len(stamps)
14231424
int64_t[:] result = np.empty(n, dtype=np.int64)

pandas/_libs/tslibs/tzconversion.pyx

+16-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cython
55
from cython import Py_ssize_t
66

77
from cpython.datetime cimport (
8-
PyDateTime_IMPORT, PyDelta_Check, datetime, tzinfo)
8+
PyDateTime_IMPORT, PyDelta_Check, datetime, timedelta, tzinfo)
99
PyDateTime_IMPORT
1010

1111
import pytz
@@ -421,23 +421,22 @@ cdef int64_t[:] _tz_convert_one_way(int64_t[:] vals, tzinfo tz, bint to_utc):
421421
converted : ndarray[int64_t]
422422
"""
423423
cdef:
424-
int64_t[:] converted, result
424+
int64_t[:] converted
425425
Py_ssize_t i, n = len(vals)
426426
int64_t val
427427

428-
if not is_utc(tz):
428+
if is_utc(tz):
429+
converted = vals
430+
elif is_tzlocal(tz):
429431
converted = np.empty(n, dtype=np.int64)
430-
if is_tzlocal(tz):
431-
for i in range(n):
432-
val = vals[i]
433-
if val == NPY_NAT:
434-
converted[i] = NPY_NAT
435-
else:
436-
converted[i] = _tz_convert_tzlocal_utc(val, tz, to_utc)
437-
else:
438-
converted = _tz_convert_dst(vals, tz, to_utc)
432+
for i in range(n):
433+
val = vals[i]
434+
if val == NPY_NAT:
435+
converted[i] = NPY_NAT
436+
else:
437+
converted[i] = _tz_convert_tzlocal_utc(val, tz, to_utc)
439438
else:
440-
converted = vals
439+
converted = _tz_convert_dst(vals, tz, to_utc)
441440

442441
return converted
443442

@@ -471,11 +470,12 @@ cdef inline int64_t _tzlocal_get_offset_components(int64_t val, tzinfo tz,
471470
npy_datetimestruct dts
472471
datetime dt
473472
int64_t delta
473+
timedelta td
474474

475475
dt64_to_dtstruct(val, &dts)
476476
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
477477
dts.min, dts.sec, dts.us)
478-
# get_utcoffset (tz.utcoffset under the hood) only makes sense if datetime
478+
# tz.utcoffset only makes sense if datetime
479479
# is _wall time_, so if val is a UTC timestamp convert to wall time
480480
if not to_utc:
481481
dt = dt.replace(tzinfo=tzutc())
@@ -484,7 +484,8 @@ cdef inline int64_t _tzlocal_get_offset_components(int64_t val, tzinfo tz,
484484
if fold is not NULL:
485485
fold[0] = dt.fold
486486

487-
return int(get_utcoffset(tz, dt).total_seconds()) * 1000000000
487+
td = tz.utcoffset(dt)
488+
return int(td.total_seconds() * 1_000_000_000)
488489

489490

490491
cdef int64_t _tz_convert_tzlocal_utc(int64_t val, tzinfo tz, bint to_utc=True,

0 commit comments

Comments
 (0)