Skip to content

Commit ca39324

Browse files
authored
Replace is_temporal checks with Cython version (#55506)
1 parent 820d5a9 commit ca39324

11 files changed

+35
-62
lines changed

pandas/_libs/lib.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
16491649
if seen_val is False and skipna:
16501650
return "empty"
16511651

1652-
if util.is_datetime64_object(val):
1652+
if cnp.is_datetime64_object(val):
16531653
if is_datetime64_array(values, skipna=skipna):
16541654
return "datetime64"
16551655

@@ -1733,7 +1733,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
17331733

17341734

17351735
cdef bint is_timedelta(object o):
1736-
return PyDelta_Check(o) or util.is_timedelta64_object(o)
1736+
return PyDelta_Check(o) or cnp.is_timedelta64_object(o)
17371737

17381738

17391739
@cython.internal
@@ -2020,7 +2020,7 @@ cpdef bint is_datetime_array(ndarray values, bint skipna=True):
20202020
@cython.internal
20212021
cdef class Datetime64Validator(DatetimeValidator):
20222022
cdef bint is_value_typed(self, object value) except -1:
2023-
return util.is_datetime64_object(value)
2023+
return cnp.is_datetime64_object(value)
20242024

20252025

20262026
# Note: only python-exposed for tests
@@ -2034,7 +2034,7 @@ cpdef bint is_datetime64_array(ndarray values, bint skipna=True):
20342034
@cython.internal
20352035
cdef class AnyDatetimeValidator(DatetimeValidator):
20362036
cdef bint is_value_typed(self, object value) except -1:
2037-
return util.is_datetime64_object(value) or (
2037+
return cnp.is_datetime64_object(value) or (
20382038
PyDateTime_Check(value) and value.tzinfo is None
20392039
)
20402040

@@ -2617,7 +2617,7 @@ def maybe_convert_objects(ndarray[object] objects,
26172617
seen.complex_ = True
26182618
if not convert_numeric:
26192619
break
2620-
elif PyDateTime_Check(val) or util.is_datetime64_object(val):
2620+
elif PyDateTime_Check(val) or cnp.is_datetime64_object(val):
26212621

26222622
# if we have an tz's attached then return the objects
26232623
if convert_non_numeric:

pandas/_libs/missing.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,17 @@ cpdef bint is_matching_na(object left, object right, bint nan_matches_none=False
120120
and util.is_complex_object(right)
121121
and util.is_nan(right)
122122
)
123-
elif util.is_datetime64_object(left):
123+
elif cnp.is_datetime64_object(left):
124124
return (
125125
get_datetime64_value(left) == NPY_NAT
126-
and util.is_datetime64_object(right)
126+
and cnp.is_datetime64_object(right)
127127
and get_datetime64_value(right) == NPY_NAT
128128
and get_datetime64_unit(left) == get_datetime64_unit(right)
129129
)
130-
elif util.is_timedelta64_object(left):
130+
elif cnp.is_timedelta64_object(left):
131131
return (
132132
get_timedelta64_value(left) == NPY_NAT
133-
and util.is_timedelta64_object(right)
133+
and cnp.is_timedelta64_object(right)
134134
and get_timedelta64_value(right) == NPY_NAT
135135
and get_datetime64_unit(left) == get_datetime64_unit(right)
136136
)
@@ -169,9 +169,9 @@ cpdef bint checknull(object val, bint inf_as_na=False):
169169
elif inf_as_na:
170170
return val == INF or val == NEGINF
171171
return False
172-
elif util.is_timedelta64_object(val):
172+
elif cnp.is_timedelta64_object(val):
173173
return get_timedelta64_value(val) == NPY_NAT
174-
elif util.is_datetime64_object(val):
174+
elif cnp.is_datetime64_object(val):
175175
return get_datetime64_value(val) == NPY_NAT
176176
else:
177177
return is_decimal_na(val)

pandas/_libs/tslib.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import_datetime()
2323
cimport numpy as cnp
2424
from numpy cimport (
2525
int64_t,
26+
is_datetime64_object,
2627
ndarray,
2728
)
2829

@@ -47,7 +48,6 @@ import_pandas_datetime()
4748

4849
from pandas._libs.tslibs.strptime cimport parse_today_now
4950
from pandas._libs.util cimport (
50-
is_datetime64_object,
5151
is_float_object,
5252
is_integer_object,
5353
)

pandas/_libs/tslibs/conversion.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from libc.math cimport log10
55
from numpy cimport (
66
int32_t,
77
int64_t,
8+
is_datetime64_object,
89
)
910

1011
cnp.import_array()
@@ -71,7 +72,6 @@ from pandas._libs.tslibs.tzconversion cimport (
7172
tz_localize_to_utc_single,
7273
)
7374
from pandas._libs.tslibs.util cimport (
74-
is_datetime64_object,
7575
is_float_object,
7676
is_integer_object,
7777
)

pandas/_libs/tslibs/nattype.pyx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _make_error_func(func_name: str, cls):
6767

6868

6969
cdef _nat_divide_op(self, other):
70-
if PyDelta_Check(other) or util.is_timedelta64_object(other) or other is c_NaT:
70+
if PyDelta_Check(other) or cnp.is_timedelta64_object(other) or other is c_NaT:
7171
return np.nan
7272
if util.is_integer_object(other) or util.is_float_object(other):
7373
return c_NaT
@@ -95,11 +95,11 @@ cdef class _NaT(datetime):
9595
__array_priority__ = 100
9696

9797
def __richcmp__(_NaT self, object other, int op):
98-
if util.is_datetime64_object(other) or PyDateTime_Check(other):
98+
if cnp.is_datetime64_object(other) or PyDateTime_Check(other):
9999
# We treat NaT as datetime-like for this comparison
100100
return op == Py_NE
101101

102-
elif util.is_timedelta64_object(other) or PyDelta_Check(other):
102+
elif cnp.is_timedelta64_object(other) or PyDelta_Check(other):
103103
# We treat NaT as timedelta-like for this comparison
104104
return op == Py_NE
105105

@@ -137,7 +137,7 @@ cdef class _NaT(datetime):
137137
return c_NaT
138138
elif PyDelta_Check(other):
139139
return c_NaT
140-
elif util.is_datetime64_object(other) or util.is_timedelta64_object(other):
140+
elif cnp.is_datetime64_object(other) or cnp.is_timedelta64_object(other):
141141
return c_NaT
142142

143143
elif util.is_integer_object(other):
@@ -175,7 +175,7 @@ cdef class _NaT(datetime):
175175
return c_NaT
176176
elif PyDelta_Check(other):
177177
return c_NaT
178-
elif util.is_datetime64_object(other) or util.is_timedelta64_object(other):
178+
elif cnp.is_datetime64_object(other) or cnp.is_timedelta64_object(other):
179179
return c_NaT
180180

181181
elif util.is_integer_object(other):
@@ -1438,7 +1438,7 @@ cdef bint is_dt64nat(object val):
14381438
"""
14391439
Is this a np.datetime64 object np.datetime64("NaT").
14401440
"""
1441-
if util.is_datetime64_object(val):
1441+
if cnp.is_datetime64_object(val):
14421442
return get_datetime64_value(val) == NPY_NAT
14431443
return False
14441444

@@ -1447,6 +1447,6 @@ cdef bint is_td64nat(object val):
14471447
"""
14481448
Is this a np.timedelta64 object np.timedelta64("NaT").
14491449
"""
1450-
if util.is_timedelta64_object(val):
1450+
if cnp.is_timedelta64_object(val):
14511451
return get_timedelta64_value(val) == NPY_NAT
14521452
return False

pandas/_libs/tslibs/offsets.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import numpy as np
2525
cimport numpy as cnp
2626
from numpy cimport (
2727
int64_t,
28+
is_datetime64_object,
2829
ndarray,
2930
)
3031

@@ -36,7 +37,6 @@ from pandas._libs.properties import cache_readonly
3637

3738
from pandas._libs.tslibs cimport util
3839
from pandas._libs.tslibs.util cimport (
39-
is_datetime64_object,
4040
is_float_object,
4141
is_integer_object,
4242
)
@@ -155,7 +155,7 @@ def apply_wraps(func):
155155
elif (
156156
isinstance(other, BaseOffset)
157157
or PyDelta_Check(other)
158-
or util.is_timedelta64_object(other)
158+
or cnp.is_timedelta64_object(other)
159159
):
160160
# timedelta path
161161
return func(self, other)
@@ -762,7 +762,7 @@ cdef class BaseOffset:
762762
TypeError if `int(n)` raises
763763
ValueError if n != int(n)
764764
"""
765-
if util.is_timedelta64_object(n):
765+
if cnp.is_timedelta64_object(n):
766766
raise TypeError(f"`n` argument must be an integer, got {type(n)}")
767767
try:
768768
nint = int(n)
@@ -1091,7 +1091,7 @@ cdef class Tick(SingleConstructorOffset):
10911091
# PyDate_Check includes date, datetime
10921092
return Timestamp(other) + self
10931093

1094-
if util.is_timedelta64_object(other) or PyDelta_Check(other):
1094+
if cnp.is_timedelta64_object(other) or PyDelta_Check(other):
10951095
return other + self.delta
10961096

10971097
raise ApplyTypeError(f"Unhandled type: {type(other).__name__}")

pandas/_libs/tslibs/period.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ cdef class _Period(PeriodMixin):
18211821
f"Period(freq={self.freqstr})")
18221822

18231823
if (
1824-
util.is_timedelta64_object(other) and
1824+
cnp.is_timedelta64_object(other) and
18251825
get_timedelta64_value(other) == NPY_NAT
18261826
):
18271827
# i.e. np.timedelta64("nat")
@@ -2810,7 +2810,7 @@ class Period(_Period):
28102810
raise ValueError("Must supply freq for datetime value")
28112811
if isinstance(dt, Timestamp):
28122812
nanosecond = dt.nanosecond
2813-
elif util.is_datetime64_object(value):
2813+
elif cnp.is_datetime64_object(value):
28142814
dt = Timestamp(value)
28152815
if freq is None:
28162816
raise ValueError("Must supply freq for datetime value")

pandas/_libs/tslibs/strptime.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ from cpython.datetime cimport (
2525
timedelta,
2626
tzinfo,
2727
)
28+
2829
from _strptime import (
2930
TimeRE as _TimeRE,
3031
_getlang,
@@ -42,6 +43,7 @@ import pytz
4243
cimport numpy as cnp
4344
from numpy cimport (
4445
int64_t,
46+
is_datetime64_object,
4547
ndarray,
4648
)
4749

@@ -69,9 +71,9 @@ from pandas._libs.tslibs.np_datetime cimport (
6971
import_pandas_datetime()
7072

7173
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
74+
7275
from pandas._libs.tslibs.timestamps cimport _Timestamp
7376
from pandas._libs.util cimport (
74-
is_datetime64_object,
7577
is_float_object,
7678
is_integer_object,
7779
)

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import numpy as np
2020
cimport numpy as cnp
2121
from numpy cimport (
2222
int64_t,
23+
is_datetime64_object,
24+
is_timedelta64_object,
2325
ndarray,
2426
)
2527

@@ -80,10 +82,8 @@ from pandas._libs.tslibs.np_datetime import (
8082
from pandas._libs.tslibs.offsets cimport is_tick_object
8183
from pandas._libs.tslibs.util cimport (
8284
is_array,
83-
is_datetime64_object,
8485
is_float_object,
8586
is_integer_object,
86-
is_timedelta64_object,
8787
)
8888

8989
from pandas._libs.tslibs.fields import (

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import numpy as np
1616
cimport numpy as cnp
1717
from numpy cimport (
1818
int64_t,
19+
is_datetime64_object,
1920
ndarray,
2021
uint8_t,
2122
)
@@ -65,7 +66,6 @@ from pandas._libs.tslibs.dtypes cimport (
6566
)
6667
from pandas._libs.tslibs.util cimport (
6768
is_array,
68-
is_datetime64_object,
6969
is_integer_object,
7070
)
7171

pandas/_libs/tslibs/util.pxd

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ cdef extern from "Python.h":
2525
from numpy cimport (
2626
float64_t,
2727
int64_t,
28+
is_timedelta64_object,
2829
)
2930

3031

@@ -51,7 +52,7 @@ cdef inline int64_t get_nat() noexcept:
5152
# --------------------------------------------------------------------
5253
# Type Checking
5354

54-
cdef inline bint is_integer_object(object obj) noexcept nogil:
55+
cdef inline bint is_integer_object(object obj) noexcept:
5556
"""
5657
Cython equivalent of
5758
@@ -121,40 +122,10 @@ cdef inline bint is_bool_object(object obj) noexcept nogil:
121122
PyObject_TypeCheck(obj, &PyBoolArrType_Type))
122123

123124

124-
cdef inline bint is_real_number_object(object obj) noexcept nogil:
125+
cdef inline bint is_real_number_object(object obj) noexcept:
125126
return is_bool_object(obj) or is_integer_object(obj) or is_float_object(obj)
126127

127128

128-
cdef inline bint is_timedelta64_object(object obj) noexcept nogil:
129-
"""
130-
Cython equivalent of `isinstance(val, np.timedelta64)`
131-
132-
Parameters
133-
----------
134-
val : object
135-
136-
Returns
137-
-------
138-
is_timedelta64 : bool
139-
"""
140-
return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)
141-
142-
143-
cdef inline bint is_datetime64_object(object obj) noexcept nogil:
144-
"""
145-
Cython equivalent of `isinstance(val, np.datetime64)`
146-
147-
Parameters
148-
----------
149-
val : object
150-
151-
Returns
152-
-------
153-
is_datetime64 : bool
154-
"""
155-
return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)
156-
157-
158129
cdef inline bint is_array(object val) noexcept:
159130
"""
160131
Cython equivalent of `isinstance(val, np.ndarray)`

0 commit comments

Comments
 (0)