Skip to content

REF: expose pandas_timedelta_to_timedeltastruct #46578

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

Merged
merged 2 commits into from
Mar 31, 2022
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
10 changes: 5 additions & 5 deletions pandas/_libs/tslibs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
]

from pandas._libs.tslibs import dtypes
from pandas._libs.tslibs.conversion import (
OutOfBoundsTimedelta,
localize_pydatetime,
)
from pandas._libs.tslibs.conversion import localize_pydatetime
from pandas._libs.tslibs.dtypes import Resolution
from pandas._libs.tslibs.nattype import (
NaT,
NaTType,
iNaT,
nat_strings,
)
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
from pandas._libs.tslibs.np_datetime import (
OutOfBoundsDatetime,
OutOfBoundsTimedelta,
)
from pandas._libs.tslibs.offsets import (
BaseOffset,
Tick,
Expand Down
14 changes: 4 additions & 10 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ from pandas._libs.tslibs.np_datetime cimport (
pydatetime_to_dt64,
)

from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
from pandas._libs.tslibs.np_datetime import (
OutOfBoundsDatetime,
OutOfBoundsTimedelta,
)

from pandas._libs.tslibs.timezones cimport (
get_dst_info,
Expand Down Expand Up @@ -85,15 +88,6 @@ DT64NS_DTYPE = np.dtype('M8[ns]')
TD64NS_DTYPE = np.dtype('m8[ns]')


class OutOfBoundsTimedelta(ValueError):
"""
Raised when encountering a timedelta value that cannot be represented
as a timedelta64[ns].
"""
# Timedelta analogue to OutOfBoundsDatetime
pass


# ----------------------------------------------------------------------
# Unit Conversion Helpers

Expand Down
6 changes: 6 additions & 0 deletions pandas/_libs/tslibs/np_datetime.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ cdef extern from "src/datetime/np_datetime.h":
npy_datetime npy_datetimestruct_to_datetime(NPY_DATETIMEUNIT fr,
npy_datetimestruct *d) nogil

void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
NPY_DATETIMEUNIT fr,
pandas_timedeltastruct *result
) nogil

cdef bint cmp_scalar(int64_t lhs, int64_t rhs, int op) except -1

Expand Down Expand Up @@ -94,3 +98,5 @@ cpdef cnp.ndarray astype_overflowsafe(
cnp.dtype dtype, # ndarray[datetime64[anyunit]]
bint copy=*,
)

cdef bint cmp_dtstructs(npy_datetimestruct* left, npy_datetimestruct* right, int op)
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/np_datetime.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np

class OutOfBoundsDatetime(ValueError): ...
class OutOfBoundsTimedelta(ValueError): ...

# only exposed for testing
def py_get_unit_from_dtype(dtype: np.dtype): ...
Expand Down
36 changes: 31 additions & 5 deletions pandas/_libs/tslibs/np_datetime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ cdef extern from "src/datetime/np_datetime.h":
int cmp_npy_datetimestruct(npy_datetimestruct *a,
npy_datetimestruct *b)

void pandas_timedelta_to_timedeltastruct(npy_timedelta val,
NPY_DATETIMEUNIT fr,
pandas_timedeltastruct *result
) nogil

# AS, FS, PS versions exist but are not imported because they are not used.
npy_datetimestruct _NS_MIN_DTS, _NS_MAX_DTS
npy_datetimestruct _US_MIN_DTS, _US_MAX_DTS
Expand Down Expand Up @@ -100,6 +95,28 @@ def py_get_unit_from_dtype(dtype):
# Comparison


cdef bint cmp_dtstructs(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this will be tested in a follow up PR when integrated into Timestamp & Timedelta?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

npy_datetimestruct* left, npy_datetimestruct* right, int op
):
cdef:
int cmp_res

cmp_res = cmp_npy_datetimestruct(left, right)
if op == Py_EQ:
return cmp_res == 0
if op == Py_NE:
return cmp_res != 0
if op == Py_GT:
return cmp_res == 1
if op == Py_LT:
return cmp_res == -1
if op == Py_GE:
return cmp_res == 1 or cmp_res == 0
else:
# i.e. op == Py_LE
return cmp_res == -1 or cmp_res == 0


cdef inline bint cmp_scalar(int64_t lhs, int64_t rhs, int op) except -1:
"""
cmp_scalar is a more performant version of PyObject_RichCompare
Expand Down Expand Up @@ -127,6 +144,15 @@ class OutOfBoundsDatetime(ValueError):
pass


class OutOfBoundsTimedelta(ValueError):
"""
Raised when encountering a timedelta value that cannot be represented
as a timedelta64[ns].
"""
# Timedelta analogue to OutOfBoundsDatetime
pass


cdef check_dts_bounds(npy_datetimestruct *dts, NPY_DATETIMEUNIT unit=NPY_FR_ns):
"""Raises OutOfBoundsDatetime if the given date is outside the range that
can be represented by nanosecond-resolution 64-bit integers."""
Expand Down
3 changes: 1 addition & 2 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ from pandas._libs.tslibs.np_datetime cimport (
pandas_timedeltastruct,
td64_to_tdstruct,
)
from pandas._libs.tslibs.np_datetime import OutOfBoundsTimedelta
from pandas._libs.tslibs.offsets cimport is_tick_object
from pandas._libs.tslibs.util cimport (
is_array,
Expand Down Expand Up @@ -188,7 +189,6 @@ cpdef int64_t delta_to_nanoseconds(delta) except? -1:
+ delta.microseconds
) * 1000
except OverflowError as err:
from pandas._libs.tslibs.conversion import OutOfBoundsTimedelta
raise OutOfBoundsTimedelta(*err.args) from err

raise TypeError(type(delta))
Expand Down Expand Up @@ -226,7 +226,6 @@ cdef object ensure_td64ns(object ts):
# NB: cython#1381 this cannot be *=
td64_value = td64_value * mult
except OverflowError as err:
from pandas._libs.tslibs.conversion import OutOfBoundsTimedelta
raise OutOfBoundsTimedelta(ts) from err

return np.timedelta64(td64_value, "ns")
Expand Down