Skip to content

Commit 5e872ce

Browse files
authored
CLN: use numpy-provided versions of util funcs (#55695)
1 parent 05f2f71 commit 5e872ce

File tree

9 files changed

+18
-52
lines changed

9 files changed

+18
-52
lines changed

pandas/_libs/missing.pyx

+6-8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ from pandas._libs.tslibs.nattype cimport (
3232
)
3333
from pandas._libs.tslibs.np_datetime cimport (
3434
get_datetime64_unit,
35-
get_datetime64_value,
36-
get_timedelta64_value,
3735
import_pandas_datetime,
3836
)
3937

@@ -122,16 +120,16 @@ cpdef bint is_matching_na(object left, object right, bint nan_matches_none=False
122120
)
123121
elif cnp.is_datetime64_object(left):
124122
return (
125-
get_datetime64_value(left) == NPY_NAT
123+
cnp.get_datetime64_value(left) == NPY_NAT
126124
and cnp.is_datetime64_object(right)
127-
and get_datetime64_value(right) == NPY_NAT
125+
and cnp.get_datetime64_value(right) == NPY_NAT
128126
and get_datetime64_unit(left) == get_datetime64_unit(right)
129127
)
130128
elif cnp.is_timedelta64_object(left):
131129
return (
132-
get_timedelta64_value(left) == NPY_NAT
130+
cnp.get_timedelta64_value(left) == NPY_NAT
133131
and cnp.is_timedelta64_object(right)
134-
and get_timedelta64_value(right) == NPY_NAT
132+
and cnp.get_timedelta64_value(right) == NPY_NAT
135133
and get_datetime64_unit(left) == get_datetime64_unit(right)
136134
)
137135
elif is_decimal_na(left):
@@ -170,9 +168,9 @@ cpdef bint checknull(object val, bint inf_as_na=False):
170168
return val == INF or val == NEGINF
171169
return False
172170
elif cnp.is_timedelta64_object(val):
173-
return get_timedelta64_value(val) == NPY_NAT
171+
return cnp.get_timedelta64_value(val) == NPY_NAT
174172
elif cnp.is_datetime64_object(val):
175-
return get_datetime64_value(val) == NPY_NAT
173+
return cnp.get_datetime64_value(val) == NPY_NAT
176174
else:
177175
return is_decimal_na(val)
178176

pandas/_libs/tslibs/conversion.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ from pandas._libs.tslibs.np_datetime cimport (
4141
convert_reso,
4242
get_conversion_factor,
4343
get_datetime64_unit,
44-
get_datetime64_value,
4544
get_implementation_bounds,
4645
import_pandas_datetime,
4746
npy_datetime,
@@ -198,7 +197,7 @@ cdef int64_t get_datetime64_nanos(object val, NPY_DATETIMEUNIT reso) except? -1:
198197
NPY_DATETIMEUNIT unit
199198
npy_datetime ival
200199

201-
ival = get_datetime64_value(val)
200+
ival = cnp.get_datetime64_value(val)
202201
if ival == NPY_NAT:
203202
return NPY_NAT
204203

pandas/_libs/tslibs/nattype.pyx

+2-6
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ from numpy cimport int64_t
2121
cnp.import_array()
2222

2323
cimport pandas._libs.tslibs.util as util
24-
from pandas._libs.tslibs.np_datetime cimport (
25-
get_datetime64_value,
26-
get_timedelta64_value,
27-
)
2824

2925
# ----------------------------------------------------------------------
3026
# Constants
@@ -1439,7 +1435,7 @@ cdef bint is_dt64nat(object val):
14391435
Is this a np.datetime64 object np.datetime64("NaT").
14401436
"""
14411437
if cnp.is_datetime64_object(val):
1442-
return get_datetime64_value(val) == NPY_NAT
1438+
return cnp.get_datetime64_value(val) == NPY_NAT
14431439
return False
14441440

14451441

@@ -1448,5 +1444,5 @@ cdef bint is_td64nat(object val):
14481444
Is this a np.timedelta64 object np.timedelta64("NaT").
14491445
"""
14501446
if cnp.is_timedelta64_object(val):
1451-
return get_timedelta64_value(val) == NPY_NAT
1447+
return cnp.get_timedelta64_value(val) == NPY_NAT
14521448
return False

pandas/_libs/tslibs/np_datetime.pxd

-7
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ cdef extern from "numpy/arrayscalars.h":
2525
npy_datetime obval
2626
PyArray_DatetimeMetaData obmeta
2727

28-
ctypedef struct PyTimedeltaScalarObject:
29-
# PyObject_HEAD
30-
npy_timedelta obval
31-
PyArray_DatetimeMetaData obmeta
32-
3328
cdef extern from "numpy/ndarraytypes.h":
3429
ctypedef struct npy_datetimestruct:
3530
int64_t year
@@ -96,8 +91,6 @@ cdef int64_t pydate_to_dt64(
9691
)
9792
cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts) noexcept
9893

99-
cdef npy_datetime get_datetime64_value(object obj) noexcept nogil
100-
cdef npy_timedelta get_timedelta64_value(object obj) noexcept nogil
10194
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil
10295

10396
cdef int string_to_dts(

pandas/_libs/tslibs/np_datetime.pyx

+1-16
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,6 @@ cdef extern from "pandas/datetime/pd_datetime.h":
5959
# ----------------------------------------------------------------------
6060
# numpy object inspection
6161

62-
cdef npy_datetime get_datetime64_value(object obj) noexcept nogil:
63-
"""
64-
returns the int64 value underlying scalar numpy datetime64 object
65-
66-
Note that to interpret this as a datetime, the corresponding unit is
67-
also needed. That can be found using `get_datetime64_unit`.
68-
"""
69-
return (<PyDatetimeScalarObject*>obj).obval
70-
71-
72-
cdef npy_timedelta get_timedelta64_value(object obj) noexcept nogil:
73-
"""
74-
returns the int64 value underlying scalar numpy timedelta64 object
75-
"""
76-
return (<PyTimedeltaScalarObject*>obj).obval
77-
7862

7963
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil:
8064
"""
@@ -278,6 +262,7 @@ cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts) noexcept:
278262
dts.ps = dts.as = 0
279263
return
280264

265+
281266
cdef int64_t pydate_to_dt64(
282267
date val, npy_datetimestruct *dts, NPY_DATETIMEUNIT reso=NPY_FR_ns
283268
):

pandas/_libs/tslibs/period.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ from pandas._libs.tslibs.np_datetime cimport (
5353
NPY_FR_D,
5454
astype_overflowsafe,
5555
check_dts_bounds,
56-
get_timedelta64_value,
5756
import_pandas_datetime,
5857
npy_datetimestruct,
5958
npy_datetimestruct_to_datetime,
@@ -1822,7 +1821,7 @@ cdef class _Period(PeriodMixin):
18221821

18231822
if (
18241823
cnp.is_timedelta64_object(other) and
1825-
get_timedelta64_value(other) == NPY_NAT
1824+
cnp.get_timedelta64_value(other) == NPY_NAT
18261825
):
18271826
# i.e. np.timedelta64("nat")
18281827
return NaT

pandas/_libs/tslibs/timedeltas.pyx

+6-7
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ from pandas._libs.tslibs.np_datetime cimport (
6363
cmp_scalar,
6464
convert_reso,
6565
get_datetime64_unit,
66-
get_timedelta64_value,
6766
get_unit_from_dtype,
6867
import_pandas_datetime,
6968
npy_datetimestruct,
@@ -261,7 +260,7 @@ cpdef int64_t delta_to_nanoseconds(
261260
"delta_to_nanoseconds does not support Y or M units, "
262261
"as their duration in nanoseconds is ambiguous."
263262
)
264-
n = get_timedelta64_value(delta)
263+
n = cnp.get_timedelta64_value(delta)
265264

266265
elif PyDelta_Check(delta):
267266
in_reso = NPY_DATETIMEUNIT.NPY_FR_us
@@ -313,7 +312,7 @@ cdef object ensure_td64ns(object ts):
313312
):
314313
unitstr = npy_unit_to_abbrev(td64_unit)
315314

316-
td64_value = get_timedelta64_value(ts)
315+
td64_value = cnp.get_timedelta64_value(ts)
317316

318317
mult = precision_from_unit(unitstr)[0]
319318
try:
@@ -484,7 +483,7 @@ cdef int64_t _item_to_timedelta64(
484483
See array_to_timedelta64.
485484
"""
486485
try:
487-
return get_timedelta64_value(convert_to_timedelta64(item, parsed_unit))
486+
return cnp.get_timedelta64_value(convert_to_timedelta64(item, parsed_unit))
488487
except ValueError as err:
489488
if errors == "coerce":
490489
return NPY_NAT
@@ -1859,7 +1858,7 @@ class Timedelta(_Timedelta):
18591858
elif is_timedelta64_object(value):
18601859
# Retain the resolution if possible, otherwise cast to the nearest
18611860
# supported resolution.
1862-
new_value = get_timedelta64_value(value)
1861+
new_value = cnp.get_timedelta64_value(value)
18631862
if new_value == NPY_NAT:
18641863
# i.e. np.timedelta64("NaT")
18651864
return NaT
@@ -2223,7 +2222,7 @@ def truediv_object_array(ndarray left, ndarray right):
22232222
td64 = left[i]
22242223
obj = right[i]
22252224

2226-
if get_timedelta64_value(td64) == NPY_NAT:
2225+
if cnp.get_timedelta64_value(td64) == NPY_NAT:
22272226
# td here should be interpreted as a td64 NaT
22282227
if _should_cast_to_timedelta(obj):
22292228
res_value = np.nan
@@ -2252,7 +2251,7 @@ def floordiv_object_array(ndarray left, ndarray right):
22522251
td64 = left[i]
22532252
obj = right[i]
22542253

2255-
if get_timedelta64_value(td64) == NPY_NAT:
2254+
if cnp.get_timedelta64_value(td64) == NPY_NAT:
22562255
# td here should be interpreted as a td64 NaT
22572256
if _should_cast_to_timedelta(obj):
22582257
res_value = np.nan

pandas/_libs/tslibs/timestamps.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ from pandas._libs.tslibs.np_datetime cimport (
8888
cmp_scalar,
8989
convert_reso,
9090
get_datetime64_unit,
91-
get_datetime64_value,
9291
get_unit_from_dtype,
9392
import_pandas_datetime,
9493
npy_datetimestruct,
@@ -307,7 +306,7 @@ cdef class _Timestamp(ABCTimestamp):
307306
NPY_DATETIMEUNIT reso
308307

309308
reso = get_datetime64_unit(dt64)
310-
value = get_datetime64_value(dt64)
309+
value = cnp.get_datetime64_value(dt64)
311310
return cls._from_value_and_reso(value, reso, None)
312311

313312
# -----------------------------------------------------------------

pandas/_libs/tslibs/util.pxd

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ cdef extern from "numpy/arrayobject.h":
3333
PyTypeObject PyFloatingArrType_Type
3434

3535
cdef extern from "numpy/ndarrayobject.h":
36-
PyTypeObject PyTimedeltaArrType_Type
37-
PyTypeObject PyDatetimeArrType_Type
3836
PyTypeObject PyComplexFloatingArrType_Type
3937
PyTypeObject PyBoolArrType_Type
4038

0 commit comments

Comments
 (0)