diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index 82f69c1dedd53..691fd05fe5ded 100644 --- a/pandas/_libs/interval.pyx +++ b/pandas/_libs/interval.pyx @@ -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"]) @@ -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 ( @@ -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) @@ -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 @@ -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 diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index e8a69891c6093..eac0b4418540e 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -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() @@ -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 diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index c2d2acd95ce43..d5fb860d9cc82 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -23,7 +23,6 @@ import_datetime() cimport numpy as cnp from numpy cimport ( int64_t, - is_datetime64_object, ndarray, ) @@ -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: diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 89b420b18a980..23352bc81e12a 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -5,7 +5,6 @@ from libc.math cimport log10 from numpy cimport ( int32_t, int64_t, - is_datetime64_object, ) cnp.import_array() @@ -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) diff --git a/pandas/_libs/tslibs/np_datetime.pxd b/pandas/_libs/tslibs/np_datetime.pxd index 9cc211b748f68..a87c3d3f0955d 100644 --- a/pandas/_libs/tslibs/np_datetime.pxd +++ b/pandas/_libs/tslibs/np_datetime.pxd @@ -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 diff --git a/pandas/_libs/tslibs/np_datetime.pyx b/pandas/_libs/tslibs/np_datetime.pyx index fa9b82fe46634..73a3540c8d6b8 100644 --- a/pandas/_libs/tslibs/np_datetime.pyx +++ b/pandas/_libs/tslibs/np_datetime.pyx @@ -28,6 +28,8 @@ cimport numpy as cnp cnp.import_array() from numpy cimport ( + PyArray_DatetimeMetaData, + PyDatetimeScalarObject, int64_t, ndarray, uint8_t, diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 7f3a72178a359..bf18a4d435573 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -25,7 +25,6 @@ import numpy as np cimport numpy as cnp from numpy cimport ( int64_t, - is_datetime64_object, ndarray, ) @@ -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: @@ -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 diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index 8966c9e81699b..acfcd1cbd217a 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -43,7 +43,6 @@ import pytz cimport numpy as cnp from numpy cimport ( int64_t, - is_datetime64_object, ndarray, ) @@ -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: diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 0a9bd2ac2770b..5852a7d95d994 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -20,8 +20,6 @@ import numpy as np cimport numpy as cnp from numpy cimport ( int64_t, - is_datetime64_object, - is_timedelta64_object, ndarray, ) @@ -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( @@ -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: @@ -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]") @@ -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 @@ -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) @@ -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 @@ -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) ) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 56a6885d4a9e0..e953945d76f45 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -16,7 +16,6 @@ import numpy as np cimport numpy as cnp from numpy cimport ( int64_t, - is_datetime64_object, ndarray, uint8_t, ) @@ -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: @@ -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__ @@ -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__ @@ -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 diff --git a/pandas/_libs/tslibs/util.pxd b/pandas/_libs/tslibs/util.pxd index ddd07276c7a7e..44ff322060dcb 100644 --- a/pandas/_libs/tslibs/util.pxd +++ b/pandas/_libs/tslibs/util.pxd @@ -23,6 +23,7 @@ cdef extern from "Python.h": from numpy cimport ( + PyArray_Check, float64_t, int64_t, is_timedelta64_object, @@ -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