Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLN: use numpy-provided versions of util funcs #55695

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ from pandas._libs.tslibs.nattype cimport (
)
from pandas._libs.tslibs.np_datetime cimport (
get_datetime64_unit,
get_datetime64_value,
get_timedelta64_value,
import_pandas_datetime,
)

Expand Down Expand Up @@ -122,16 +120,16 @@ cpdef bint is_matching_na(object left, object right, bint nan_matches_none=False
)
elif cnp.is_datetime64_object(left):
return (
get_datetime64_value(left) == NPY_NAT
cnp.get_datetime64_value(left) == NPY_NAT
and cnp.is_datetime64_object(right)
and get_datetime64_value(right) == NPY_NAT
and cnp.get_datetime64_value(right) == NPY_NAT
and get_datetime64_unit(left) == get_datetime64_unit(right)
)
elif cnp.is_timedelta64_object(left):
return (
get_timedelta64_value(left) == NPY_NAT
cnp.get_timedelta64_value(left) == NPY_NAT
and cnp.is_timedelta64_object(right)
and get_timedelta64_value(right) == NPY_NAT
and cnp.get_timedelta64_value(right) == NPY_NAT
and get_datetime64_unit(left) == get_datetime64_unit(right)
)
elif is_decimal_na(left):
Expand Down Expand Up @@ -170,9 +168,9 @@ cpdef bint checknull(object val, bint inf_as_na=False):
return val == INF or val == NEGINF
return False
elif cnp.is_timedelta64_object(val):
return get_timedelta64_value(val) == NPY_NAT
return cnp.get_timedelta64_value(val) == NPY_NAT
elif cnp.is_datetime64_object(val):
return get_datetime64_value(val) == NPY_NAT
return cnp.get_datetime64_value(val) == NPY_NAT
else:
return is_decimal_na(val)

Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ from pandas._libs.tslibs.np_datetime cimport (
convert_reso,
get_conversion_factor,
get_datetime64_unit,
get_datetime64_value,
get_implementation_bounds,
import_pandas_datetime,
npy_datetime,
Expand Down Expand Up @@ -198,7 +197,7 @@ cdef int64_t get_datetime64_nanos(object val, NPY_DATETIMEUNIT reso) except? -1:
NPY_DATETIMEUNIT unit
npy_datetime ival

ival = get_datetime64_value(val)
ival = cnp.get_datetime64_value(val)
if ival == NPY_NAT:
return NPY_NAT

Expand Down
8 changes: 2 additions & 6 deletions pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ from numpy cimport int64_t
cnp.import_array()

cimport pandas._libs.tslibs.util as util
from pandas._libs.tslibs.np_datetime cimport (
get_datetime64_value,
get_timedelta64_value,
)

# ----------------------------------------------------------------------
# Constants
Expand Down Expand Up @@ -1439,7 +1435,7 @@ cdef bint is_dt64nat(object val):
Is this a np.datetime64 object np.datetime64("NaT").
"""
if cnp.is_datetime64_object(val):
return get_datetime64_value(val) == NPY_NAT
return cnp.get_datetime64_value(val) == NPY_NAT
return False


Expand All @@ -1448,5 +1444,5 @@ cdef bint is_td64nat(object val):
Is this a np.timedelta64 object np.timedelta64("NaT").
"""
if cnp.is_timedelta64_object(val):
return get_timedelta64_value(val) == NPY_NAT
return cnp.get_timedelta64_value(val) == NPY_NAT
return False
7 changes: 0 additions & 7 deletions pandas/_libs/tslibs/np_datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ cdef extern from "numpy/arrayscalars.h":
npy_datetime obval
PyArray_DatetimeMetaData obmeta

ctypedef struct PyTimedeltaScalarObject:
# PyObject_HEAD
npy_timedelta obval
PyArray_DatetimeMetaData obmeta

cdef extern from "numpy/ndarraytypes.h":
ctypedef struct npy_datetimestruct:
int64_t year
Expand Down Expand Up @@ -96,8 +91,6 @@ cdef int64_t pydate_to_dt64(
)
cdef void pydate_to_dtstruct(date val, npy_datetimestruct *dts) noexcept

cdef npy_datetime get_datetime64_value(object obj) noexcept nogil
cdef npy_timedelta get_timedelta64_value(object obj) noexcept nogil
cdef NPY_DATETIMEUNIT get_datetime64_unit(object obj) noexcept nogil

cdef int string_to_dts(
Expand Down
17 changes: 1 addition & 16 deletions pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ cdef extern from "pandas/datetime/pd_datetime.h":
# ----------------------------------------------------------------------
# numpy object inspection

cdef npy_datetime get_datetime64_value(object obj) noexcept nogil:
"""
returns the int64 value underlying scalar numpy datetime64 object

Note that to interpret this as a datetime, the corresponding unit is
also needed. That can be found using `get_datetime64_unit`.
"""
return (<PyDatetimeScalarObject*>obj).obval


cdef npy_timedelta get_timedelta64_value(object obj) noexcept nogil:
"""
returns the int64 value underlying scalar numpy timedelta64 object
"""
return (<PyTimedeltaScalarObject*>obj).obval


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


cdef int64_t pydate_to_dt64(
date val, npy_datetimestruct *dts, NPY_DATETIMEUNIT reso=NPY_FR_ns
):
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ from pandas._libs.tslibs.np_datetime cimport (
NPY_FR_D,
astype_overflowsafe,
check_dts_bounds,
get_timedelta64_value,
import_pandas_datetime,
npy_datetimestruct,
npy_datetimestruct_to_datetime,
Expand Down Expand Up @@ -1822,7 +1821,7 @@ cdef class _Period(PeriodMixin):

if (
cnp.is_timedelta64_object(other) and
get_timedelta64_value(other) == NPY_NAT
cnp.get_timedelta64_value(other) == NPY_NAT
):
# i.e. np.timedelta64("nat")
return NaT
Expand Down
13 changes: 6 additions & 7 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ from pandas._libs.tslibs.np_datetime cimport (
cmp_scalar,
convert_reso,
get_datetime64_unit,
get_timedelta64_value,
get_unit_from_dtype,
import_pandas_datetime,
npy_datetimestruct,
Expand Down Expand Up @@ -261,7 +260,7 @@ cpdef int64_t delta_to_nanoseconds(
"delta_to_nanoseconds does not support Y or M units, "
"as their duration in nanoseconds is ambiguous."
)
n = get_timedelta64_value(delta)
n = cnp.get_timedelta64_value(delta)

elif PyDelta_Check(delta):
in_reso = NPY_DATETIMEUNIT.NPY_FR_us
Expand Down Expand Up @@ -313,7 +312,7 @@ cdef object ensure_td64ns(object ts):
):
unitstr = npy_unit_to_abbrev(td64_unit)

td64_value = get_timedelta64_value(ts)
td64_value = cnp.get_timedelta64_value(ts)

mult = precision_from_unit(unitstr)[0]
try:
Expand Down Expand Up @@ -484,7 +483,7 @@ cdef int64_t _item_to_timedelta64(
See array_to_timedelta64.
"""
try:
return get_timedelta64_value(convert_to_timedelta64(item, parsed_unit))
return cnp.get_timedelta64_value(convert_to_timedelta64(item, parsed_unit))
except ValueError as err:
if errors == "coerce":
return NPY_NAT
Expand Down Expand Up @@ -1859,7 +1858,7 @@ class Timedelta(_Timedelta):
elif is_timedelta64_object(value):
# Retain the resolution if possible, otherwise cast to the nearest
# supported resolution.
new_value = get_timedelta64_value(value)
new_value = cnp.get_timedelta64_value(value)
if new_value == NPY_NAT:
# i.e. np.timedelta64("NaT")
return NaT
Expand Down Expand Up @@ -2223,7 +2222,7 @@ def truediv_object_array(ndarray left, ndarray right):
td64 = left[i]
obj = right[i]

if get_timedelta64_value(td64) == NPY_NAT:
if cnp.get_timedelta64_value(td64) == NPY_NAT:
# td here should be interpreted as a td64 NaT
if _should_cast_to_timedelta(obj):
res_value = np.nan
Expand Down Expand Up @@ -2252,7 +2251,7 @@ def floordiv_object_array(ndarray left, ndarray right):
td64 = left[i]
obj = right[i]

if get_timedelta64_value(td64) == NPY_NAT:
if cnp.get_timedelta64_value(td64) == NPY_NAT:
# td here should be interpreted as a td64 NaT
if _should_cast_to_timedelta(obj):
res_value = np.nan
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ from pandas._libs.tslibs.np_datetime cimport (
cmp_scalar,
convert_reso,
get_datetime64_unit,
get_datetime64_value,
get_unit_from_dtype,
import_pandas_datetime,
npy_datetimestruct,
Expand Down Expand Up @@ -307,7 +306,7 @@ cdef class _Timestamp(ABCTimestamp):
NPY_DATETIMEUNIT reso

reso = get_datetime64_unit(dt64)
value = get_datetime64_value(dt64)
value = cnp.get_datetime64_value(dt64)
return cls._from_value_and_reso(value, reso, None)

# -----------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/util.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ cdef extern from "numpy/arrayobject.h":
PyTypeObject PyFloatingArrType_Type

cdef extern from "numpy/ndarrayobject.h":
PyTypeObject PyTimedeltaArrType_Type
PyTypeObject PyDatetimeArrType_Type
PyTypeObject PyComplexFloatingArrType_Type
PyTypeObject PyBoolArrType_Type

Expand Down
Loading