Skip to content

Commit 1f2d54a

Browse files
authored
CLN/TYP: assorted (pandas-dev#46568)
1 parent bea02f3 commit 1f2d54a

16 files changed

+51
-40
lines changed

pandas/_libs/algos.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ def is_monotonic(ndarray[numeric_object_t, ndim=1] arr, bint timelike):
750750
n = len(arr)
751751

752752
if n == 1:
753-
if arr[0] != arr[0] or (timelike and <int64_t>arr[0] == NPY_NAT):
753+
if arr[0] != arr[0] or (numeric_object_t is int64_t and timelike and arr[0] == NPY_NAT):
754754
# single value is NaN
755755
return False, False, True
756756
else:

pandas/_libs/indexing.pyx

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ cdef class NDFrameIndexerBase:
22
"""
33
A base class for _NDFrameIndexer for fast instantiation and attribute access.
44
"""
5+
cdef:
6+
Py_ssize_t _ndim
7+
58
cdef public:
69
str name
7-
object obj, _ndim
10+
object obj
811

912
def __init__(self, name: str, obj):
1013
self.obj = obj
1114
self.name = name
12-
self._ndim = None
15+
self._ndim = -1
1316

1417
@property
1518
def ndim(self) -> int:
1619
# Delay `ndim` instantiation until required as reading it
1720
# from `obj` isn't entirely cheap.
1821
ndim = self._ndim
19-
if ndim is None:
22+
if ndim == -1:
2023
ndim = self._ndim = self.obj.ndim
2124
if ndim > 2:
2225
raise ValueError( # pragma: no cover

pandas/_libs/internals.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ cpdef update_blklocs_and_blknos(
544544
"""
545545
cdef:
546546
Py_ssize_t i
547-
cnp.npy_intp length = len(blklocs) + 1
547+
cnp.npy_intp length = blklocs.shape[0] + 1
548548
ndarray[intp_t, ndim=1] new_blklocs, new_blknos
549549

550550
# equiv: new_blklocs = np.empty(length, dtype=np.intp)

pandas/_libs/tslib.pyx

+5-5
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def array_with_unit_to_datetime(
218218
"""
219219
cdef:
220220
Py_ssize_t i, j, n=len(values)
221-
int64_t m
221+
int64_t mult
222222
int prec = 0
223223
ndarray[float64_t] fvalues
224224
bint is_ignore = errors=='ignore'
@@ -242,7 +242,7 @@ def array_with_unit_to_datetime(
242242
)
243243
return result, tz
244244

245-
m, _ = precision_from_unit(unit)
245+
mult, _ = precision_from_unit(unit)
246246

247247
if is_raise:
248248
# try a quick conversion to i8/f8
@@ -254,7 +254,7 @@ def array_with_unit_to_datetime(
254254
# fill missing values by comparing to NPY_NAT
255255
mask = iresult == NPY_NAT
256256
iresult[mask] = 0
257-
fvalues = iresult.astype("f8") * m
257+
fvalues = iresult.astype("f8") * mult
258258
need_to_iterate = False
259259

260260
if not need_to_iterate:
@@ -265,10 +265,10 @@ def array_with_unit_to_datetime(
265265
raise OutOfBoundsDatetime(f"cannot convert input with unit '{unit}'")
266266

267267
if values.dtype.kind in ["i", "u"]:
268-
result = (iresult * m).astype("M8[ns]")
268+
result = (iresult * mult).astype("M8[ns]")
269269

270270
elif values.dtype.kind == "f":
271-
fresult = (values * m).astype("f8")
271+
fresult = (values * mult).astype("f8")
272272
fresult[mask] = 0
273273
if prec:
274274
fresult = round(fresult, prec)

pandas/_libs/tslibs/conversion.pxd

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ cdef class _TSObject:
1919
bint fold
2020

2121

22-
cdef convert_to_tsobject(object ts, tzinfo tz, str unit,
23-
bint dayfirst, bint yearfirst,
24-
int32_t nanos=*)
22+
cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
23+
bint dayfirst, bint yearfirst,
24+
int32_t nanos=*)
2525

2626
cdef _TSObject convert_datetime_to_tsobject(datetime ts, tzinfo tz,
2727
int32_t nanos=*)

pandas/_libs/tslibs/conversion.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ cdef class _TSObject:
345345
self.fold = 0
346346

347347

348-
cdef convert_to_tsobject(object ts, tzinfo tz, str unit,
349-
bint dayfirst, bint yearfirst, int32_t nanos=0):
348+
cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
349+
bint dayfirst, bint yearfirst, int32_t nanos=0):
350350
"""
351351
Extract datetime and int64 from any of:
352352
- np.int64 (with unit providing a possible modifier)

pandas/_libs/tslibs/dtypes.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT
55

66
cdef str npy_unit_to_abbrev(NPY_DATETIMEUNIT unit)
77
cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil
8-
cdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=*)
8+
cdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=*) except? -1
99

1010
cdef dict attrname_to_abbrevs
1111

pandas/_libs/tslibs/dtypes.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil:
307307
return NPY_DATETIMEUNIT.NPY_FR_D
308308

309309

310-
cdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns):
310+
cdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns) except? -1:
311311
"""
312312
How many of the given time units fit into a single day?
313313
"""

pandas/_libs/tslibs/offsets.pyx

+7-5
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,7 @@ cdef class WeekOfMonthMixin(SingleConstructorOffset):
19101910

19111911
shifted = shift_month(other, months, "start")
19121912
to_day = self._get_offset_day(shifted)
1913-
return shift_day(shifted, to_day - shifted.day)
1913+
return _shift_day(shifted, to_day - shifted.day)
19141914

19151915
def is_on_offset(self, dt: datetime) -> bool:
19161916
if self.normalize and not _is_normalized(dt):
@@ -3132,7 +3132,7 @@ cdef class FY5253Quarter(FY5253Mixin):
31323132
qtr_lens = self.get_weeks(norm)
31333133

31343134
# check that qtr_lens is consistent with self._offset addition
3135-
end = shift_day(start, days=7 * sum(qtr_lens))
3135+
end = _shift_day(start, days=7 * sum(qtr_lens))
31363136
assert self._offset.is_on_offset(end), (start, end, qtr_lens)
31373137

31383138
tdelta = norm - start
@@ -3173,7 +3173,7 @@ cdef class FY5253Quarter(FY5253Mixin):
31733173
# Note: we always have 0 <= n < 4
31743174
weeks = sum(qtr_lens[:n])
31753175
if weeks:
3176-
res = shift_day(res, days=weeks * 7)
3176+
res = _shift_day(res, days=weeks * 7)
31773177

31783178
return res
31793179

@@ -3210,7 +3210,7 @@ cdef class FY5253Quarter(FY5253Mixin):
32103210

32113211
current = next_year_end
32123212
for qtr_len in qtr_lens:
3213-
current = shift_day(current, days=qtr_len * 7)
3213+
current = _shift_day(current, days=qtr_len * 7)
32143214
if dt == current:
32153215
return True
32163216
return False
@@ -3729,7 +3729,7 @@ cpdef to_offset(freq):
37293729
# ----------------------------------------------------------------------
37303730
# RelativeDelta Arithmetic
37313731

3732-
def shift_day(other: datetime, days: int) -> datetime:
3732+
cdef datetime _shift_day(datetime other, int days):
37333733
"""
37343734
Increment the datetime `other` by the given number of days, retaining
37353735
the time-portion of the datetime. For tz-naive datetimes this is
@@ -3915,6 +3915,8 @@ cdef inline void _shift_quarters(const int64_t[:] dtindex,
39153915
out[i] = dtstruct_to_dt64(&dts)
39163916

39173917

3918+
@cython.wraparound(False)
3919+
@cython.boundscheck(False)
39183920
cdef ndarray[int64_t] _shift_bdays(const int64_t[:] i8other, int periods):
39193921
"""
39203922
Implementation of BusinessDay.apply_offset.

pandas/_libs/tslibs/timedeltas.pxd

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ cdef class _Timedelta(timedelta):
1515
int64_t _d, _h, _m, _s, _ms, _us, _ns
1616

1717
cpdef timedelta to_pytimedelta(_Timedelta self)
18-
cpdef bint _has_ns(self)
18+
cdef bint _has_ns(self)
19+
cdef _ensure_components(_Timedelta self)

pandas/_libs/tslibs/timedeltas.pyx

+7-4
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def ints_to_pytimedelta(const int64_t[:] arr, box=False):
154154
cdef:
155155
Py_ssize_t i, n = len(arr)
156156
int64_t value
157-
object[:] result = np.empty(n, dtype=object)
157+
object[::1] result = np.empty(n, dtype=object)
158158

159159
for i in range(n):
160160

@@ -892,10 +892,10 @@ cdef class _Timedelta(timedelta):
892892

893893
return cmp_scalar(self.value, ots.value, op)
894894

895-
cpdef bint _has_ns(self):
895+
cdef bint _has_ns(self):
896896
return self.value % 1000 != 0
897897

898-
def _ensure_components(_Timedelta self):
898+
cdef _ensure_components(_Timedelta self):
899899
"""
900900
compute the components
901901
"""
@@ -1160,7 +1160,10 @@ cdef class _Timedelta(timedelta):
11601160
converted : string of a Timedelta
11611161

11621162
"""
1163-
cdef object sign, seconds_pretty, subs, fmt, comp_dict
1163+
cdef:
1164+
str sign, fmt
1165+
dict comp_dict
1166+
object subs
11641167

11651168
self._ensure_components()
11661169

pandas/_libs/tslibs/timestamps.pxd

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ from numpy cimport int64_t
66

77
from pandas._libs.tslibs.base cimport ABCTimestamp
88
from pandas._libs.tslibs.np_datetime cimport npy_datetimestruct
9+
from pandas._libs.tslibs.offsets cimport BaseOffset
910

1011

11-
cdef object create_timestamp_from_ts(int64_t value,
12-
npy_datetimestruct dts,
13-
tzinfo tz, object freq, bint fold)
12+
cdef _Timestamp create_timestamp_from_ts(int64_t value,
13+
npy_datetimestruct dts,
14+
tzinfo tz, BaseOffset freq, bint fold)
1415

1516

1617
cdef class _Timestamp(ABCTimestamp):
1718
cdef readonly:
1819
int64_t value, nanosecond
19-
object _freq
20+
BaseOffset _freq
2021

2122
cdef bint _get_start_end_field(self, str field, freq)
2223
cdef _get_date_name_field(self, str field, object locale)

pandas/_libs/tslibs/timestamps.pyx

+4-3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ from pandas._libs.tslibs.np_datetime cimport (
8282
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
8383

8484
from pandas._libs.tslibs.offsets cimport (
85+
BaseOffset,
8586
is_offset_object,
8687
to_offset,
8788
)
@@ -113,9 +114,9 @@ _no_input = object()
113114
# ----------------------------------------------------------------------
114115

115116

116-
cdef inline object create_timestamp_from_ts(int64_t value,
117-
npy_datetimestruct dts,
118-
tzinfo tz, object freq, bint fold):
117+
cdef inline _Timestamp create_timestamp_from_ts(int64_t value,
118+
npy_datetimestruct dts,
119+
tzinfo tz, BaseOffset freq, bint fold):
119120
""" convenience routine to construct a Timestamp from its parts """
120121
cdef _Timestamp ts_base
121122
ts_base = _Timestamp.__new__(Timestamp, dts.year, dts.month,

pandas/_libs/tslibs/timezones.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ cdef object _get_utc_trans_times_from_dateutil_tz(tzinfo tz):
230230
return new_trans
231231

232232

233-
cdef int64_t[:] unbox_utcoffsets(object transinfo):
233+
cdef int64_t[::1] unbox_utcoffsets(object transinfo):
234234
cdef:
235235
Py_ssize_t i, sz
236-
int64_t[:] arr
236+
int64_t[::1] arr
237237

238238
sz = len(transinfo)
239239
arr = np.empty(sz, dtype='i8')

pandas/_libs/tslibs/tzconversion.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ cdef int64_t _tz_localize_using_tzinfo_api(
610610
int64_t val, tzinfo tz, bint to_utc=True, bint* fold=NULL
611611
) except? -1:
612612
"""
613-
Convert the i8 representation of a datetime from a general-cast timezone to
613+
Convert the i8 representation of a datetime from a general-case timezone to
614614
UTC, or vice-versa using the datetime/tzinfo API.
615615
616616
Private, not intended for use outside of tslibs.tzconversion.

pandas/core/arrays/datetimes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2006,10 +2006,10 @@ def sequence_to_datetimes(data, require_iso8601: bool = False) -> DatetimeArray:
20062006
def _sequence_to_dt64ns(
20072007
data,
20082008
dtype=None,
2009-
copy=False,
2009+
copy: bool = False,
20102010
tz=None,
2011-
dayfirst=False,
2012-
yearfirst=False,
2011+
dayfirst: bool = False,
2012+
yearfirst: bool = False,
20132013
ambiguous="raise",
20142014
*,
20152015
allow_mixed: bool = False,

0 commit comments

Comments
 (0)