diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 23c8066b973f8..38b34b4bb853c 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1649,7 +1649,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str: if seen_val is False and skipna: return "empty" - if util.is_datetime64_object(val): + if cnp.is_datetime64_object(val): if is_datetime64_array(values, skipna=skipna): return "datetime64" @@ -1733,7 +1733,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str: cdef bint is_timedelta(object o): - return PyDelta_Check(o) or util.is_timedelta64_object(o) + return PyDelta_Check(o) or cnp.is_timedelta64_object(o) @cython.internal @@ -2020,7 +2020,7 @@ cpdef bint is_datetime_array(ndarray values, bint skipna=True): @cython.internal cdef class Datetime64Validator(DatetimeValidator): cdef bint is_value_typed(self, object value) except -1: - return util.is_datetime64_object(value) + return cnp.is_datetime64_object(value) # Note: only python-exposed for tests @@ -2034,7 +2034,7 @@ cpdef bint is_datetime64_array(ndarray values, bint skipna=True): @cython.internal cdef class AnyDatetimeValidator(DatetimeValidator): cdef bint is_value_typed(self, object value) except -1: - return util.is_datetime64_object(value) or ( + return cnp.is_datetime64_object(value) or ( PyDateTime_Check(value) and value.tzinfo is None ) @@ -2617,7 +2617,7 @@ def maybe_convert_objects(ndarray[object] objects, seen.complex_ = True if not convert_numeric: break - elif PyDateTime_Check(val) or util.is_datetime64_object(val): + elif PyDateTime_Check(val) or cnp.is_datetime64_object(val): # if we have an tz's attached then return the objects if convert_non_numeric: diff --git a/pandas/_libs/missing.pyx b/pandas/_libs/missing.pyx index 8ef59b46ca25f..981d9cc9cbcb3 100644 --- a/pandas/_libs/missing.pyx +++ b/pandas/_libs/missing.pyx @@ -120,17 +120,17 @@ cpdef bint is_matching_na(object left, object right, bint nan_matches_none=False and util.is_complex_object(right) and util.is_nan(right) ) - elif util.is_datetime64_object(left): + elif cnp.is_datetime64_object(left): return ( get_datetime64_value(left) == NPY_NAT - and util.is_datetime64_object(right) + and cnp.is_datetime64_object(right) and get_datetime64_value(right) == NPY_NAT and get_datetime64_unit(left) == get_datetime64_unit(right) ) - elif util.is_timedelta64_object(left): + elif cnp.is_timedelta64_object(left): return ( get_timedelta64_value(left) == NPY_NAT - and util.is_timedelta64_object(right) + and cnp.is_timedelta64_object(right) and get_timedelta64_value(right) == NPY_NAT and get_datetime64_unit(left) == get_datetime64_unit(right) ) @@ -169,9 +169,9 @@ cpdef bint checknull(object val, bint inf_as_na=False): elif inf_as_na: return val == INF or val == NEGINF return False - elif util.is_timedelta64_object(val): + elif cnp.is_timedelta64_object(val): return get_timedelta64_value(val) == NPY_NAT - elif util.is_datetime64_object(val): + elif cnp.is_datetime64_object(val): return get_datetime64_value(val) == NPY_NAT else: return is_decimal_na(val) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 55454e6c58755..3694a358cfb56 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -23,6 +23,7 @@ import_datetime() cimport numpy as cnp from numpy cimport ( int64_t, + is_datetime64_object, ndarray, ) @@ -47,7 +48,6 @@ import_pandas_datetime() from pandas._libs.tslibs.strptime cimport parse_today_now from pandas._libs.util cimport ( - is_datetime64_object, is_float_object, is_integer_object, ) diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 1f4bb8af4542f..a1c59489b3d38 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -5,6 +5,7 @@ from libc.math cimport log10 from numpy cimport ( int32_t, int64_t, + is_datetime64_object, ) cnp.import_array() @@ -71,7 +72,6 @@ from pandas._libs.tslibs.tzconversion cimport ( tz_localize_to_utc_single, ) from pandas._libs.tslibs.util cimport ( - is_datetime64_object, is_float_object, is_integer_object, ) diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 9f9549b93fefe..2ed54ab71182a 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -67,7 +67,7 @@ def _make_error_func(func_name: str, cls): cdef _nat_divide_op(self, other): - if PyDelta_Check(other) or util.is_timedelta64_object(other) or other is c_NaT: + if PyDelta_Check(other) or cnp.is_timedelta64_object(other) or other is c_NaT: return np.nan if util.is_integer_object(other) or util.is_float_object(other): return c_NaT @@ -95,11 +95,11 @@ cdef class _NaT(datetime): __array_priority__ = 100 def __richcmp__(_NaT self, object other, int op): - if util.is_datetime64_object(other) or PyDateTime_Check(other): + if cnp.is_datetime64_object(other) or PyDateTime_Check(other): # We treat NaT as datetime-like for this comparison return op == Py_NE - elif util.is_timedelta64_object(other) or PyDelta_Check(other): + elif cnp.is_timedelta64_object(other) or PyDelta_Check(other): # We treat NaT as timedelta-like for this comparison return op == Py_NE @@ -137,7 +137,7 @@ cdef class _NaT(datetime): return c_NaT elif PyDelta_Check(other): return c_NaT - elif util.is_datetime64_object(other) or util.is_timedelta64_object(other): + elif cnp.is_datetime64_object(other) or cnp.is_timedelta64_object(other): return c_NaT elif util.is_integer_object(other): @@ -175,7 +175,7 @@ cdef class _NaT(datetime): return c_NaT elif PyDelta_Check(other): return c_NaT - elif util.is_datetime64_object(other) or util.is_timedelta64_object(other): + elif cnp.is_datetime64_object(other) or cnp.is_timedelta64_object(other): return c_NaT elif util.is_integer_object(other): @@ -1438,7 +1438,7 @@ cdef bint is_dt64nat(object val): """ Is this a np.datetime64 object np.datetime64("NaT"). """ - if util.is_datetime64_object(val): + if cnp.is_datetime64_object(val): return get_datetime64_value(val) == NPY_NAT return False @@ -1447,6 +1447,6 @@ cdef bint is_td64nat(object val): """ Is this a np.timedelta64 object np.timedelta64("NaT"). """ - if util.is_timedelta64_object(val): + if cnp.is_timedelta64_object(val): return get_timedelta64_value(val) == NPY_NAT return False diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index f20073766bc3a..6f442926499a3 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -25,6 +25,7 @@ import numpy as np cimport numpy as cnp from numpy cimport ( int64_t, + is_datetime64_object, ndarray, ) @@ -36,7 +37,6 @@ from pandas._libs.properties import cache_readonly from pandas._libs.tslibs cimport util from pandas._libs.tslibs.util cimport ( - is_datetime64_object, is_float_object, is_integer_object, ) @@ -155,7 +155,7 @@ def apply_wraps(func): elif ( isinstance(other, BaseOffset) or PyDelta_Check(other) - or util.is_timedelta64_object(other) + or cnp.is_timedelta64_object(other) ): # timedelta path return func(self, other) @@ -762,7 +762,7 @@ cdef class BaseOffset: TypeError if `int(n)` raises ValueError if n != int(n) """ - if util.is_timedelta64_object(n): + if cnp.is_timedelta64_object(n): raise TypeError(f"`n` argument must be an integer, got {type(n)}") try: nint = int(n) @@ -1091,7 +1091,7 @@ cdef class Tick(SingleConstructorOffset): # PyDate_Check includes date, datetime return Timestamp(other) + self - if util.is_timedelta64_object(other) or PyDelta_Check(other): + if cnp.is_timedelta64_object(other) or PyDelta_Check(other): return other + self.delta raise ApplyTypeError(f"Unhandled type: {type(other).__name__}") diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index d305f27dd1090..518577895ec9b 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1821,7 +1821,7 @@ cdef class _Period(PeriodMixin): f"Period(freq={self.freqstr})") if ( - util.is_timedelta64_object(other) and + cnp.is_timedelta64_object(other) and get_timedelta64_value(other) == NPY_NAT ): # i.e. np.timedelta64("nat") @@ -2810,7 +2810,7 @@ class Period(_Period): raise ValueError("Must supply freq for datetime value") if isinstance(dt, Timestamp): nanosecond = dt.nanosecond - elif util.is_datetime64_object(value): + elif cnp.is_datetime64_object(value): dt = Timestamp(value) if freq is None: raise ValueError("Must supply freq for datetime value") diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index e6924a4e24dff..866181246a284 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -25,6 +25,7 @@ from cpython.datetime cimport ( timedelta, tzinfo, ) + from _strptime import ( TimeRE as _TimeRE, _getlang, @@ -42,6 +43,7 @@ import pytz cimport numpy as cnp from numpy cimport ( int64_t, + is_datetime64_object, ndarray, ) @@ -69,9 +71,9 @@ from pandas._libs.tslibs.np_datetime cimport ( import_pandas_datetime() from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime + from pandas._libs.tslibs.timestamps cimport _Timestamp from pandas._libs.util cimport ( - is_datetime64_object, is_float_object, is_integer_object, ) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 5e124b89eab5e..bfa0a9e0b1864 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -20,6 +20,8 @@ import numpy as np cimport numpy as cnp from numpy cimport ( int64_t, + is_datetime64_object, + is_timedelta64_object, ndarray, ) @@ -80,10 +82,8 @@ from pandas._libs.tslibs.np_datetime import ( from pandas._libs.tslibs.offsets cimport is_tick_object from pandas._libs.tslibs.util cimport ( is_array, - is_datetime64_object, is_float_object, is_integer_object, - is_timedelta64_object, ) from pandas._libs.tslibs.fields import ( diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 66b1cec63e9e9..bf25eaeff19a5 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -16,6 +16,7 @@ import numpy as np cimport numpy as cnp from numpy cimport ( int64_t, + is_datetime64_object, ndarray, uint8_t, ) @@ -65,7 +66,6 @@ from pandas._libs.tslibs.dtypes cimport ( ) from pandas._libs.tslibs.util cimport ( is_array, - is_datetime64_object, is_integer_object, ) diff --git a/pandas/_libs/tslibs/util.pxd b/pandas/_libs/tslibs/util.pxd index 519d3fc939efa..61812bafc16d2 100644 --- a/pandas/_libs/tslibs/util.pxd +++ b/pandas/_libs/tslibs/util.pxd @@ -25,6 +25,7 @@ cdef extern from "Python.h": from numpy cimport ( float64_t, int64_t, + is_timedelta64_object, ) @@ -51,7 +52,7 @@ cdef inline int64_t get_nat() noexcept: # -------------------------------------------------------------------- # Type Checking -cdef inline bint is_integer_object(object obj) noexcept nogil: +cdef inline bint is_integer_object(object obj) noexcept: """ Cython equivalent of @@ -121,40 +122,10 @@ cdef inline bint is_bool_object(object obj) noexcept nogil: PyObject_TypeCheck(obj, &PyBoolArrType_Type)) -cdef inline bint is_real_number_object(object obj) noexcept nogil: +cdef inline bint is_real_number_object(object obj) noexcept: return is_bool_object(obj) or is_integer_object(obj) or is_float_object(obj) -cdef inline bint is_timedelta64_object(object obj) noexcept nogil: - """ - Cython equivalent of `isinstance(val, np.timedelta64)` - - Parameters - ---------- - val : object - - Returns - ------- - is_timedelta64 : bool - """ - return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type) - - -cdef inline bint is_datetime64_object(object obj) noexcept nogil: - """ - Cython equivalent of `isinstance(val, np.datetime64)` - - Parameters - ---------- - val : object - - Returns - ------- - is_datetime64 : bool - """ - return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type) - - cdef inline bint is_array(object val) noexcept: """ Cython equivalent of `isinstance(val, np.ndarray)`