Skip to content

Commit a8b2989

Browse files
authored
REF: use numpy cimports (#55995)
* REF: get npy_timedelta, npy_datetime from numpy * standardize is_datetime64_object usage * REF: cimport PyArray_DatetimeMetaData from numpy * REF: cimport more from numpy * cimport more from numpy
1 parent 500b57c commit a8b2989

11 files changed

+26
-48
lines changed

pandas/_libs/interval.pyx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ from pandas._libs.tslibs.timezones cimport tz_compare
3939
from pandas._libs.tslibs.util cimport (
4040
is_float_object,
4141
is_integer_object,
42-
is_timedelta64_object,
4342
)
4443

4544
VALID_CLOSED = frozenset(["left", "right", "both", "neither"])
@@ -493,7 +492,7 @@ cdef class Interval(IntervalMixin):
493492
if (
494493
isinstance(y, numbers.Number)
495494
or PyDelta_Check(y)
496-
or is_timedelta64_object(y)
495+
or cnp.is_timedelta64_object(y)
497496
):
498497
return Interval(self.left + y, self.right + y, closed=self.closed)
499498
elif (
@@ -503,7 +502,7 @@ cdef class Interval(IntervalMixin):
503502
and (
504503
isinstance(self, numbers.Number)
505504
or PyDelta_Check(self)
506-
or is_timedelta64_object(self)
505+
or cnp.is_timedelta64_object(self)
507506
)
508507
):
509508
return Interval(y.left + self, y.right + self, closed=y.closed)
@@ -513,7 +512,7 @@ cdef class Interval(IntervalMixin):
513512
if (
514513
isinstance(other, numbers.Number)
515514
or PyDelta_Check(other)
516-
or is_timedelta64_object(other)
515+
or cnp.is_timedelta64_object(other)
517516
):
518517
return Interval(self.left + other, self.right + other, closed=self.closed)
519518
return NotImplemented
@@ -522,7 +521,7 @@ cdef class Interval(IntervalMixin):
522521
if (
523522
isinstance(y, numbers.Number)
524523
or PyDelta_Check(y)
525-
or is_timedelta64_object(y)
524+
or cnp.is_timedelta64_object(y)
526525
):
527526
return Interval(self.left - y, self.right - y, closed=self.closed)
528527
return NotImplemented

pandas/_libs/lib.pyx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ cdef extern from "numpy/arrayobject.h":
9292
PyTypeObject PySignedIntegerArrType_Type
9393
PyTypeObject PyUnsignedIntegerArrType_Type
9494

95-
cdef extern from "numpy/ndarrayobject.h":
96-
bint PyArray_CheckScalar(obj) nogil
97-
9895
cdef extern from "pandas/parser/pd_parser.h":
9996
int floatify(object, float64_t *result, int *maybe_int) except -1
10097
void PandasParser_IMPORT()
@@ -272,7 +269,7 @@ cdef int64_t get_itemsize(object val):
272269
-------
273270
is_ndarray : bool
274271
"""
275-
if PyArray_CheckScalar(val):
272+
if cnp.PyArray_CheckScalar(val):
276273
return cnp.PyArray_DescrFromScalar(val).itemsize
277274
else:
278275
return -1

pandas/_libs/tslib.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import_datetime()
2323
cimport numpy as cnp
2424
from numpy cimport (
2525
int64_t,
26-
is_datetime64_object,
2726
ndarray,
2827
)
2928

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

516-
elif is_datetime64_object(val):
515+
elif cnp.is_datetime64_object(val):
517516
item_reso = get_supported_reso(get_datetime64_unit(val))
518517
state.update_creso(item_reso)
519518
if infer_reso:

pandas/_libs/tslibs/conversion.pyx

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

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

286285
if checknull_with_nat_and_na(ts):
287286
obj.value = NPY_NAT
288-
elif is_datetime64_object(ts):
287+
elif cnp.is_datetime64_object(ts):
289288
reso = get_supported_reso(get_datetime64_unit(ts))
290289
obj.creso = reso
291290
obj.value = get_datetime64_nanos(ts, reso)

pandas/_libs/tslibs/np_datetime.pxd

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,12 @@ from cpython.datetime cimport (
66
from numpy cimport (
77
int32_t,
88
int64_t,
9+
npy_datetime,
10+
npy_timedelta,
911
)
1012

1113

1214
# TODO(cython3): most of these can be cimported directly from numpy
13-
cdef extern from "numpy/ndarrayobject.h":
14-
ctypedef int64_t npy_timedelta
15-
ctypedef int64_t npy_datetime
16-
17-
cdef extern from "numpy/ndarraytypes.h":
18-
ctypedef struct PyArray_DatetimeMetaData:
19-
NPY_DATETIMEUNIT base
20-
int64_t num
21-
22-
cdef extern from "numpy/arrayscalars.h":
23-
ctypedef struct PyDatetimeScalarObject:
24-
# PyObject_HEAD
25-
npy_datetime obval
26-
PyArray_DatetimeMetaData obmeta
27-
2815
cdef extern from "numpy/ndarraytypes.h":
2916
ctypedef struct npy_datetimestruct:
3017
int64_t year

pandas/_libs/tslibs/np_datetime.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ cimport numpy as cnp
2828

2929
cnp.import_array()
3030
from numpy cimport (
31+
PyArray_DatetimeMetaData,
32+
PyDatetimeScalarObject,
3133
int64_t,
3234
ndarray,
3335
uint8_t,

pandas/_libs/tslibs/offsets.pyx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import numpy as np
2525
cimport numpy as cnp
2626
from numpy cimport (
2727
int64_t,
28-
is_datetime64_object,
2928
ndarray,
3029
)
3130

@@ -159,7 +158,7 @@ def apply_wraps(func):
159158
):
160159
# timedelta path
161160
return func(self, other)
162-
elif is_datetime64_object(other) or PyDate_Check(other):
161+
elif cnp.is_datetime64_object(other) or PyDate_Check(other):
163162
# PyDate_Check includes date, datetime
164163
other = Timestamp(other)
165164
else:
@@ -1087,7 +1086,7 @@ cdef class Tick(SingleConstructorOffset):
10871086
return other + self.delta
10881087
elif other is NaT:
10891088
return NaT
1090-
elif is_datetime64_object(other) or PyDate_Check(other):
1089+
elif cnp.is_datetime64_object(other) or PyDate_Check(other):
10911090
# PyDate_Check includes date, datetime
10921091
return Timestamp(other) + self
10931092

pandas/_libs/tslibs/strptime.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import pytz
4343
cimport numpy as cnp
4444
from numpy cimport (
4545
int64_t,
46-
is_datetime64_object,
4746
ndarray,
4847
)
4948

@@ -377,7 +376,7 @@ def array_strptime(
377376
creso = state.creso
378377
iresult[i] = pydate_to_dt64(val, &dts, reso=creso)
379378
continue
380-
elif is_datetime64_object(val):
379+
elif cnp.is_datetime64_object(val):
381380
item_reso = get_supported_reso(get_datetime64_unit(val))
382381
state.update_creso(item_reso)
383382
if infer_reso:

pandas/_libs/tslibs/timedeltas.pyx

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

@@ -253,7 +251,7 @@ cpdef int64_t delta_to_nanoseconds(
253251
n = delta._value
254252
in_reso = delta._creso
255253

256-
elif is_timedelta64_object(delta):
254+
elif cnp.is_timedelta64_object(delta):
257255
in_reso = get_datetime64_unit(delta)
258256
if in_reso == NPY_DATETIMEUNIT.NPY_FR_Y or in_reso == NPY_DATETIMEUNIT.NPY_FR_M:
259257
raise ValueError(
@@ -347,7 +345,7 @@ cdef convert_to_timedelta64(object ts, str unit):
347345
ts = ts.as_unit("ns").asm8
348346
else:
349347
ts = np.timedelta64(ts._value, "ns")
350-
elif is_timedelta64_object(ts):
348+
elif cnp.is_timedelta64_object(ts):
351349
ts = ensure_td64ns(ts)
352350
elif is_integer_object(ts):
353351
if ts == NPY_NAT:
@@ -367,7 +365,7 @@ cdef convert_to_timedelta64(object ts, str unit):
367365

368366
if PyDelta_Check(ts):
369367
ts = np.timedelta64(delta_to_nanoseconds(ts), "ns")
370-
elif not is_timedelta64_object(ts):
368+
elif not cnp.is_timedelta64_object(ts):
371369
raise TypeError(f"Invalid type for timedelta scalar: {type(ts)}")
372370
return ts.astype("timedelta64[ns]")
373371

@@ -764,7 +762,7 @@ def _binary_op_method_timedeltalike(op, name):
764762
if other is NaT:
765763
return NaT
766764

767-
elif is_datetime64_object(other) or (
765+
elif cnp.is_datetime64_object(other) or (
768766
PyDateTime_Check(other) and not isinstance(other, ABCTimestamp)
769767
):
770768
# this case is for a datetime object that is specifically
@@ -1853,7 +1851,7 @@ class Timedelta(_Timedelta):
18531851
return cls._from_value_and_reso(
18541852
new_value, reso=NPY_DATETIMEUNIT.NPY_FR_us
18551853
)
1856-
elif is_timedelta64_object(value):
1854+
elif cnp.is_timedelta64_object(value):
18571855
# Retain the resolution if possible, otherwise cast to the nearest
18581856
# supported resolution.
18591857
new_value = cnp.get_timedelta64_value(value)
@@ -1904,7 +1902,7 @@ class Timedelta(_Timedelta):
19041902
f"float, timedelta or convertible, not {type(value).__name__}"
19051903
)
19061904

1907-
if is_timedelta64_object(value):
1905+
if cnp.is_timedelta64_object(value):
19081906
value = value.view("i8")
19091907

19101908
# nat
@@ -2279,7 +2277,7 @@ cdef bint is_any_td_scalar(object obj):
22792277
bool
22802278
"""
22812279
return (
2282-
PyDelta_Check(obj) or is_timedelta64_object(obj) or is_tick_object(obj)
2280+
PyDelta_Check(obj) or cnp.is_timedelta64_object(obj) or is_tick_object(obj)
22832281
)
22842282

22852283

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import numpy as np
1616
cimport numpy as cnp
1717
from numpy cimport (
1818
int64_t,
19-
is_datetime64_object,
2019
ndarray,
2120
uint8_t,
2221
)
@@ -329,7 +328,7 @@ cdef class _Timestamp(ABCTimestamp):
329328
ots = other
330329
elif other is NaT:
331330
return op == Py_NE
332-
elif is_datetime64_object(other):
331+
elif cnp.is_datetime64_object(other):
333332
ots = Timestamp(other)
334333
elif PyDateTime_Check(other):
335334
if self.nanosecond == 0:
@@ -510,7 +509,7 @@ cdef class _Timestamp(ABCTimestamp):
510509

511510
# coerce if necessary if we are a Timestamp-like
512511
if (PyDateTime_Check(self)
513-
and (PyDateTime_Check(other) or is_datetime64_object(other))):
512+
and (PyDateTime_Check(other) or cnp.is_datetime64_object(other))):
514513
# both_timestamps is to determine whether Timedelta(self - other)
515514
# should raise the OOB error, or fall back returning a timedelta.
516515
# TODO(cython3): clean out the bits that moved to __rsub__
@@ -549,7 +548,7 @@ cdef class _Timestamp(ABCTimestamp):
549548
# We get here in stata tests, fall back to stdlib datetime
550549
# method and return stdlib timedelta object
551550
pass
552-
elif is_datetime64_object(self):
551+
elif cnp.is_datetime64_object(self):
553552
# GH#28286 cython semantics for __rsub__, `other` is actually
554553
# the Timestamp
555554
# TODO(cython3): remove this, this moved to __rsub__
@@ -565,7 +564,7 @@ cdef class _Timestamp(ABCTimestamp):
565564
# We get here in stata tests, fall back to stdlib datetime
566565
# method and return stdlib timedelta object
567566
pass
568-
elif is_datetime64_object(other):
567+
elif cnp.is_datetime64_object(other):
569568
return type(self)(other) - self
570569
return NotImplemented
571570

pandas/_libs/tslibs/util.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cdef extern from "Python.h":
2323

2424

2525
from numpy cimport (
26+
PyArray_Check,
2627
float64_t,
2728
int64_t,
2829
is_timedelta64_object,
@@ -37,7 +38,6 @@ cdef extern from "numpy/ndarrayobject.h":
3738
PyTypeObject PyBoolArrType_Type
3839

3940
bint PyArray_IsIntegerScalar(obj) nogil
40-
bint PyArray_Check(obj) nogil
4141

4242
cdef extern from "numpy/npy_common.h":
4343
int64_t NPY_MIN_INT64

0 commit comments

Comments
 (0)