Skip to content

REF: use numpy cimports #55995

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
Nov 16, 2023
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
9 changes: 4 additions & 5 deletions pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ from pandas._libs.tslibs.timezones cimport tz_compare
from pandas._libs.tslibs.util cimport (
is_float_object,
is_integer_object,
is_timedelta64_object,
)

VALID_CLOSED = frozenset(["left", "right", "both", "neither"])
Expand Down Expand Up @@ -493,7 +492,7 @@ cdef class Interval(IntervalMixin):
if (
isinstance(y, numbers.Number)
or PyDelta_Check(y)
or is_timedelta64_object(y)
or cnp.is_timedelta64_object(y)
):
return Interval(self.left + y, self.right + y, closed=self.closed)
elif (
Expand All @@ -503,7 +502,7 @@ cdef class Interval(IntervalMixin):
and (
isinstance(self, numbers.Number)
or PyDelta_Check(self)
or is_timedelta64_object(self)
or cnp.is_timedelta64_object(self)
)
):
return Interval(y.left + self, y.right + self, closed=y.closed)
Expand All @@ -513,7 +512,7 @@ cdef class Interval(IntervalMixin):
if (
isinstance(other, numbers.Number)
or PyDelta_Check(other)
or is_timedelta64_object(other)
or cnp.is_timedelta64_object(other)
):
return Interval(self.left + other, self.right + other, closed=self.closed)
return NotImplemented
Expand All @@ -522,7 +521,7 @@ cdef class Interval(IntervalMixin):
if (
isinstance(y, numbers.Number)
or PyDelta_Check(y)
or is_timedelta64_object(y)
or cnp.is_timedelta64_object(y)
):
return Interval(self.left - y, self.right - y, closed=self.closed)
return NotImplemented
Expand Down
5 changes: 1 addition & 4 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ cdef extern from "numpy/arrayobject.h":
PyTypeObject PySignedIntegerArrType_Type
PyTypeObject PyUnsignedIntegerArrType_Type

cdef extern from "numpy/ndarrayobject.h":
bint PyArray_CheckScalar(obj) nogil

cdef extern from "pandas/parser/pd_parser.h":
int floatify(object, float64_t *result, int *maybe_int) except -1
void PandasParser_IMPORT()
Expand Down Expand Up @@ -272,7 +269,7 @@ cdef int64_t get_itemsize(object val):
-------
is_ndarray : bool
"""
if PyArray_CheckScalar(val):
if cnp.PyArray_CheckScalar(val):
return cnp.PyArray_DescrFromScalar(val).itemsize
else:
return -1
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import_datetime()
cimport numpy as cnp
from numpy cimport (
int64_t,
is_datetime64_object,
ndarray,
)

Expand Down Expand Up @@ -513,7 +512,7 @@ cpdef array_to_datetime(
iresult[i] = pydate_to_dt64(val, &dts, reso=creso)
state.found_other = True

elif is_datetime64_object(val):
elif cnp.is_datetime64_object(val):
item_reso = get_supported_reso(get_datetime64_unit(val))
state.update_creso(item_reso)
if infer_reso:
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ from libc.math cimport log10
from numpy cimport (
int32_t,
int64_t,
is_datetime64_object,
)

cnp.import_array()
Expand Down Expand Up @@ -285,7 +284,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,

if checknull_with_nat_and_na(ts):
obj.value = NPY_NAT
elif is_datetime64_object(ts):
elif cnp.is_datetime64_object(ts):
reso = get_supported_reso(get_datetime64_unit(ts))
obj.creso = reso
obj.value = get_datetime64_nanos(ts, reso)
Expand Down
17 changes: 2 additions & 15 deletions pandas/_libs/tslibs/np_datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,12 @@ from cpython.datetime cimport (
from numpy cimport (
int32_t,
int64_t,
npy_datetime,
npy_timedelta,
)


# TODO(cython3): most of these can be cimported directly from numpy
cdef extern from "numpy/ndarrayobject.h":
ctypedef int64_t npy_timedelta
ctypedef int64_t npy_datetime

cdef extern from "numpy/ndarraytypes.h":
ctypedef struct PyArray_DatetimeMetaData:
NPY_DATETIMEUNIT base
int64_t num

cdef extern from "numpy/arrayscalars.h":
ctypedef struct PyDatetimeScalarObject:
# PyObject_HEAD
npy_datetime obval
PyArray_DatetimeMetaData obmeta

cdef extern from "numpy/ndarraytypes.h":
ctypedef struct npy_datetimestruct:
int64_t year
Expand Down
2 changes: 2 additions & 0 deletions pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ cimport numpy as cnp

cnp.import_array()
from numpy cimport (
PyArray_DatetimeMetaData,
PyDatetimeScalarObject,
int64_t,
ndarray,
uint8_t,
Expand Down
5 changes: 2 additions & 3 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import numpy as np
cimport numpy as cnp
from numpy cimport (
int64_t,
is_datetime64_object,
ndarray,
)

Expand Down Expand Up @@ -159,7 +158,7 @@ def apply_wraps(func):
):
# timedelta path
return func(self, other)
elif is_datetime64_object(other) or PyDate_Check(other):
elif cnp.is_datetime64_object(other) or PyDate_Check(other):
# PyDate_Check includes date, datetime
other = Timestamp(other)
else:
Expand Down Expand Up @@ -1087,7 +1086,7 @@ cdef class Tick(SingleConstructorOffset):
return other + self.delta
elif other is NaT:
return NaT
elif is_datetime64_object(other) or PyDate_Check(other):
elif cnp.is_datetime64_object(other) or PyDate_Check(other):
# PyDate_Check includes date, datetime
return Timestamp(other) + self

Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/strptime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import pytz
cimport numpy as cnp
from numpy cimport (
int64_t,
is_datetime64_object,
ndarray,
)

Expand Down Expand Up @@ -377,7 +376,7 @@ def array_strptime(
creso = state.creso
iresult[i] = pydate_to_dt64(val, &dts, reso=creso)
continue
elif is_datetime64_object(val):
elif cnp.is_datetime64_object(val):
item_reso = get_supported_reso(get_datetime64_unit(val))
state.update_creso(item_reso)
if infer_reso:
Expand Down
16 changes: 7 additions & 9 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import numpy as np
cimport numpy as cnp
from numpy cimport (
int64_t,
is_datetime64_object,
is_timedelta64_object,
ndarray,
)

Expand Down Expand Up @@ -253,7 +251,7 @@ cpdef int64_t delta_to_nanoseconds(
n = delta._value
in_reso = delta._creso

elif is_timedelta64_object(delta):
elif cnp.is_timedelta64_object(delta):
in_reso = get_datetime64_unit(delta)
if in_reso == NPY_DATETIMEUNIT.NPY_FR_Y or in_reso == NPY_DATETIMEUNIT.NPY_FR_M:
raise ValueError(
Expand Down Expand Up @@ -347,7 +345,7 @@ cdef convert_to_timedelta64(object ts, str unit):
ts = ts.as_unit("ns").asm8
else:
ts = np.timedelta64(ts._value, "ns")
elif is_timedelta64_object(ts):
elif cnp.is_timedelta64_object(ts):
ts = ensure_td64ns(ts)
elif is_integer_object(ts):
if ts == NPY_NAT:
Expand All @@ -367,7 +365,7 @@ cdef convert_to_timedelta64(object ts, str unit):

if PyDelta_Check(ts):
ts = np.timedelta64(delta_to_nanoseconds(ts), "ns")
elif not is_timedelta64_object(ts):
elif not cnp.is_timedelta64_object(ts):
raise TypeError(f"Invalid type for timedelta scalar: {type(ts)}")
return ts.astype("timedelta64[ns]")

Expand Down Expand Up @@ -764,7 +762,7 @@ def _binary_op_method_timedeltalike(op, name):
if other is NaT:
return NaT

elif is_datetime64_object(other) or (
elif cnp.is_datetime64_object(other) or (
PyDateTime_Check(other) and not isinstance(other, ABCTimestamp)
):
# this case is for a datetime object that is specifically
Expand Down Expand Up @@ -1853,7 +1851,7 @@ class Timedelta(_Timedelta):
return cls._from_value_and_reso(
new_value, reso=NPY_DATETIMEUNIT.NPY_FR_us
)
elif is_timedelta64_object(value):
elif cnp.is_timedelta64_object(value):
# Retain the resolution if possible, otherwise cast to the nearest
# supported resolution.
new_value = cnp.get_timedelta64_value(value)
Expand Down Expand Up @@ -1904,7 +1902,7 @@ class Timedelta(_Timedelta):
f"float, timedelta or convertible, not {type(value).__name__}"
)

if is_timedelta64_object(value):
if cnp.is_timedelta64_object(value):
value = value.view("i8")

# nat
Expand Down Expand Up @@ -2279,7 +2277,7 @@ cdef bint is_any_td_scalar(object obj):
bool
"""
return (
PyDelta_Check(obj) or is_timedelta64_object(obj) or is_tick_object(obj)
PyDelta_Check(obj) or cnp.is_timedelta64_object(obj) or is_tick_object(obj)
)


Expand Down
9 changes: 4 additions & 5 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import numpy as np
cimport numpy as cnp
from numpy cimport (
int64_t,
is_datetime64_object,
ndarray,
uint8_t,
)
Expand Down Expand Up @@ -329,7 +328,7 @@ cdef class _Timestamp(ABCTimestamp):
ots = other
elif other is NaT:
return op == Py_NE
elif is_datetime64_object(other):
elif cnp.is_datetime64_object(other):
ots = Timestamp(other)
elif PyDateTime_Check(other):
if self.nanosecond == 0:
Expand Down Expand Up @@ -510,7 +509,7 @@ cdef class _Timestamp(ABCTimestamp):

# coerce if necessary if we are a Timestamp-like
if (PyDateTime_Check(self)
and (PyDateTime_Check(other) or is_datetime64_object(other))):
and (PyDateTime_Check(other) or cnp.is_datetime64_object(other))):
# both_timestamps is to determine whether Timedelta(self - other)
# should raise the OOB error, or fall back returning a timedelta.
# TODO(cython3): clean out the bits that moved to __rsub__
Expand Down Expand Up @@ -549,7 +548,7 @@ cdef class _Timestamp(ABCTimestamp):
# We get here in stata tests, fall back to stdlib datetime
# method and return stdlib timedelta object
pass
elif is_datetime64_object(self):
elif cnp.is_datetime64_object(self):
# GH#28286 cython semantics for __rsub__, `other` is actually
# the Timestamp
# TODO(cython3): remove this, this moved to __rsub__
Expand All @@ -565,7 +564,7 @@ cdef class _Timestamp(ABCTimestamp):
# We get here in stata tests, fall back to stdlib datetime
# method and return stdlib timedelta object
pass
elif is_datetime64_object(other):
elif cnp.is_datetime64_object(other):
return type(self)(other) - self
return NotImplemented

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ cdef extern from "Python.h":


from numpy cimport (
PyArray_Check,
float64_t,
int64_t,
is_timedelta64_object,
Expand All @@ -37,7 +38,6 @@ cdef extern from "numpy/ndarrayobject.h":
PyTypeObject PyBoolArrType_Type

bint PyArray_IsIntegerScalar(obj) nogil
bint PyArray_Check(obj) nogil

cdef extern from "numpy/npy_common.h":
int64_t NPY_MIN_INT64
Expand Down