Skip to content

CLN/TYP: assorted #46568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ def is_monotonic(ndarray[numeric_object_t, ndim=1] arr, bint timelike):
n = len(arr)

if n == 1:
if arr[0] != arr[0] or (timelike and <int64_t>arr[0] == NPY_NAT):
if arr[0] != arr[0] or (numeric_object_t is int64_t and timelike and arr[0] == NPY_NAT):
# single value is NaN
return False, False, True
else:
Expand Down
9 changes: 6 additions & 3 deletions pandas/_libs/indexing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ cdef class NDFrameIndexerBase:
"""
A base class for _NDFrameIndexer for fast instantiation and attribute access.
"""
cdef:
Py_ssize_t _ndim

cdef public:
str name
object obj, _ndim
object obj

def __init__(self, name: str, obj):
self.obj = obj
self.name = name
self._ndim = None
self._ndim = -1

@property
def ndim(self) -> int:
# Delay `ndim` instantiation until required as reading it
# from `obj` isn't entirely cheap.
ndim = self._ndim
if ndim is None:
if ndim == -1:
ndim = self._ndim = self.obj.ndim
if ndim > 2:
raise ValueError( # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/internals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ cpdef update_blklocs_and_blknos(
"""
cdef:
Py_ssize_t i
cnp.npy_intp length = len(blklocs) + 1
cnp.npy_intp length = blklocs.shape[0] + 1
ndarray[intp_t, ndim=1] new_blklocs, new_blknos

# equiv: new_blklocs = np.empty(length, dtype=np.intp)
Expand Down
10 changes: 5 additions & 5 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def array_with_unit_to_datetime(
"""
cdef:
Py_ssize_t i, j, n=len(values)
int64_t m
int64_t mult
int prec = 0
ndarray[float64_t] fvalues
bint is_ignore = errors=='ignore'
Expand All @@ -242,7 +242,7 @@ def array_with_unit_to_datetime(
)
return result, tz

m, _ = precision_from_unit(unit)
mult, _ = precision_from_unit(unit)

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

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

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

elif values.dtype.kind == "f":
fresult = (values * m).astype("f8")
fresult = (values * mult).astype("f8")
fresult[mask] = 0
if prec:
fresult = round(fresult, prec)
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/conversion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ cdef class _TSObject:
bint fold


cdef convert_to_tsobject(object ts, tzinfo tz, str unit,
bint dayfirst, bint yearfirst,
int32_t nanos=*)
cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
bint dayfirst, bint yearfirst,
int32_t nanos=*)

cdef _TSObject convert_datetime_to_tsobject(datetime ts, tzinfo tz,
int32_t nanos=*)
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ cdef class _TSObject:
self.fold = 0


cdef convert_to_tsobject(object ts, tzinfo tz, str unit,
bint dayfirst, bint yearfirst, int32_t nanos=0):
cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
bint dayfirst, bint yearfirst, int32_t nanos=0):
"""
Extract datetime and int64 from any of:
- np.int64 (with unit providing a possible modifier)
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/dtypes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ from pandas._libs.tslibs.np_datetime cimport NPY_DATETIMEUNIT

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

cdef dict attrname_to_abbrevs

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/dtypes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) nogil:
return NPY_DATETIMEUNIT.NPY_FR_D


cdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns):
cdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns) except? -1:
"""
How many of the given time units fit into a single day?
"""
Expand Down
12 changes: 7 additions & 5 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,7 @@ cdef class WeekOfMonthMixin(SingleConstructorOffset):

shifted = shift_month(other, months, "start")
to_day = self._get_offset_day(shifted)
return shift_day(shifted, to_day - shifted.day)
return _shift_day(shifted, to_day - shifted.day)

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

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

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

return res

Expand Down Expand Up @@ -3210,7 +3210,7 @@ cdef class FY5253Quarter(FY5253Mixin):

current = next_year_end
for qtr_len in qtr_lens:
current = shift_day(current, days=qtr_len * 7)
current = _shift_day(current, days=qtr_len * 7)
if dt == current:
return True
return False
Expand Down Expand Up @@ -3729,7 +3729,7 @@ cpdef to_offset(freq):
# ----------------------------------------------------------------------
# RelativeDelta Arithmetic

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


@cython.wraparound(False)
@cython.boundscheck(False)
cdef ndarray[int64_t] _shift_bdays(const int64_t[:] i8other, int periods):
"""
Implementation of BusinessDay.apply_offset.
Expand Down
3 changes: 2 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ cdef class _Timedelta(timedelta):
int64_t _d, _h, _m, _s, _ms, _us, _ns

cpdef timedelta to_pytimedelta(_Timedelta self)
cpdef bint _has_ns(self)
cdef bint _has_ns(self)
cdef _ensure_components(_Timedelta self)
11 changes: 7 additions & 4 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def ints_to_pytimedelta(const int64_t[:] arr, box=False):
cdef:
Py_ssize_t i, n = len(arr)
int64_t value
object[:] result = np.empty(n, dtype=object)
object[::1] result = np.empty(n, dtype=object)

for i in range(n):

Expand Down Expand Up @@ -892,10 +892,10 @@ cdef class _Timedelta(timedelta):

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

cpdef bint _has_ns(self):
cdef bint _has_ns(self):
return self.value % 1000 != 0

def _ensure_components(_Timedelta self):
cdef _ensure_components(_Timedelta self):
"""
compute the components
"""
Expand Down Expand Up @@ -1160,7 +1160,10 @@ cdef class _Timedelta(timedelta):
converted : string of a Timedelta

"""
cdef object sign, seconds_pretty, subs, fmt, comp_dict
cdef:
str sign, fmt
dict comp_dict
object subs

self._ensure_components()

Expand Down
9 changes: 5 additions & 4 deletions pandas/_libs/tslibs/timestamps.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ from numpy cimport int64_t

from pandas._libs.tslibs.base cimport ABCTimestamp
from pandas._libs.tslibs.np_datetime cimport npy_datetimestruct
from pandas._libs.tslibs.offsets cimport BaseOffset


cdef object create_timestamp_from_ts(int64_t value,
npy_datetimestruct dts,
tzinfo tz, object freq, bint fold)
cdef _Timestamp create_timestamp_from_ts(int64_t value,
npy_datetimestruct dts,
tzinfo tz, BaseOffset freq, bint fold)


cdef class _Timestamp(ABCTimestamp):
cdef readonly:
int64_t value, nanosecond
object _freq
BaseOffset _freq

cdef bint _get_start_end_field(self, str field, freq)
cdef _get_date_name_field(self, str field, object locale)
Expand Down
7 changes: 4 additions & 3 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ from pandas._libs.tslibs.np_datetime cimport (
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime

from pandas._libs.tslibs.offsets cimport (
BaseOffset,
is_offset_object,
to_offset,
)
Expand Down Expand Up @@ -113,9 +114,9 @@ _no_input = object()
# ----------------------------------------------------------------------


cdef inline object create_timestamp_from_ts(int64_t value,
npy_datetimestruct dts,
tzinfo tz, object freq, bint fold):
cdef inline _Timestamp create_timestamp_from_ts(int64_t value,
npy_datetimestruct dts,
tzinfo tz, BaseOffset freq, bint fold):
""" convenience routine to construct a Timestamp from its parts """
cdef _Timestamp ts_base
ts_base = _Timestamp.__new__(Timestamp, dts.year, dts.month,
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ cdef object _get_utc_trans_times_from_dateutil_tz(tzinfo tz):
return new_trans


cdef int64_t[:] unbox_utcoffsets(object transinfo):
cdef int64_t[::1] unbox_utcoffsets(object transinfo):
cdef:
Py_ssize_t i, sz
int64_t[:] arr
int64_t[::1] arr

sz = len(transinfo)
arr = np.empty(sz, dtype='i8')
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ cdef int64_t _tz_localize_using_tzinfo_api(
int64_t val, tzinfo tz, bint to_utc=True, bint* fold=NULL
) except? -1:
"""
Convert the i8 representation of a datetime from a general-cast timezone to
Convert the i8 representation of a datetime from a general-case timezone to
UTC, or vice-versa using the datetime/tzinfo API.

Private, not intended for use outside of tslibs.tzconversion.
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2006,10 +2006,10 @@ def sequence_to_datetimes(data, require_iso8601: bool = False) -> DatetimeArray:
def _sequence_to_dt64ns(
data,
dtype=None,
copy=False,
copy: bool = False,
tz=None,
dayfirst=False,
yearfirst=False,
dayfirst: bool = False,
yearfirst: bool = False,
ambiguous="raise",
*,
allow_mixed: bool = False,
Expand Down