Skip to content

BUG: Adds missing raises for numpy.timedelta64[M/Y] in pandas.Timedelta #53421

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 27 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
41c9adc
adds raise for numpy Y and M conversion in Timedelta
mcgeestocks May 27, 2023
2221543
Merge branch 'main' into timedelta-raise
mcgeestocks May 28, 2023
90d34f5
fix failing tests
mcgeestocks May 29, 2023
36d2abf
Merge branch 'timedelta-raise' of github.com:mcgeestocks/pandas-mcgee…
mcgeestocks May 29, 2023
5db101b
Merge branch 'main' into timedelta-raise
mcgeestocks May 29, 2023
d5be260
Merge branch 'main' into timedelta-raise
mcgeestocks May 30, 2023
3259eb9
update raise lang
mcgeestocks May 30, 2023
cfc3451
Merge remote-tracking branch 'upstream/main' into timedelta-raise
mcgeestocks Jun 5, 2023
20dcb16
alternative approach
mcgeestocks Jun 5, 2023
cc48ef7
add missing periods
mcgeestocks Jun 7, 2023
af689be
Merge remote-tracking branch 'upstream/main' into timedelta-raise
mcgeestocks Jun 7, 2023
0589d34
Merge branch 'main' into timedelta-raise
mcgeestocks Jun 8, 2023
77a256c
switch to cimport
mcgeestocks Jun 9, 2023
97be59c
add cython directive
mcgeestocks Jun 9, 2023
2ae5b72
switch to inline cython directive
mcgeestocks Jun 9, 2023
86ace60
Merge branch 'main' into timedelta-raise
mcgeestocks Jun 9, 2023
69e1f96
remove unsupported td operation
mcgeestocks Jun 9, 2023
09fb208
adds whatnew update
mcgeestocks Jun 9, 2023
8fd2a31
Merge branch 'main' into timedelta-raise
mcgeestocks Jun 11, 2023
c603860
Merge branch 'main' into timedelta-raise
mcgeestocks Jun 12, 2023
6ccaadd
Merge remote-tracking branch 'upstream/main' into timedelta-raise
mcgeestocks Jun 19, 2023
cc1ec83
remove whitespace
mcgeestocks Jun 19, 2023
3057fe8
adds test
mcgeestocks Jun 19, 2023
73bcf11
Merge branch 'main' into timedelta-raise
mcgeestocks Jun 19, 2023
2afb107
Merge branch 'main' into timedelta-raise
mcgeestocks Jun 19, 2023
5b840a2
Merge branch 'main' into timedelta-raise
mcgeestocks Jun 19, 2023
1deec72
Merge branch 'main' into timedelta-raise
mcgeestocks Jun 20, 2023
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
3 changes: 0 additions & 3 deletions doc/source/user_guide/timedeltas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,6 @@ an alternative is to divide by another timedelta object. Note that division by t
# to days
td / np.timedelta64(1, "D")

# to months (these are constant months)
td / np.timedelta64(1, "M")

Dividing or multiplying a ``timedelta64[ns]`` Series by an integer or integer Series
yields another ``timedelta64[ns]`` dtypes Series.

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ Timedelta
^^^^^^^^^
- :meth:`TimedeltaIndex.map` with ``na_action="ignore"`` now works as expected (:issue:`51644`)
- Bug in :class:`TimedeltaIndex` division or multiplication leading to ``.freq`` of "0 Days" instead of ``None`` (:issue:`51575`)
- Bug in :class:`Timedelta` with Numpy timedelta64 objects not properly raising ``ValueError`` (:issue:`52806`)
- Bug in :meth:`Timedelta.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsTimedelta`` (:issue:`51494`)
- Bug in :meth:`arrays.TimedeltaArray.map` and :meth:`TimedeltaIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`)
-
Expand Down
1 change: 1 addition & 0 deletions pandas/_libs/tslibs/dtypes.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ cdef NPY_DATETIMEUNIT freq_group_code_to_npy_unit(int freq) noexcept nogil
cpdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=*) except? -1
cpdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except? -1
cpdef NPY_DATETIMEUNIT get_supported_reso(NPY_DATETIMEUNIT reso)
cpdef bint is_supported_unit(NPY_DATETIMEUNIT reso)

cdef dict attrname_to_abbrevs
cdef dict npy_unit_to_attrname
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/dtypes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ cpdef NPY_DATETIMEUNIT get_supported_reso(NPY_DATETIMEUNIT reso):
return reso


def is_supported_unit(NPY_DATETIMEUNIT reso):
cpdef bint is_supported_unit(NPY_DATETIMEUNIT reso):
return (
reso == NPY_DATETIMEUNIT.NPY_FR_ns
or reso == NPY_DATETIMEUNIT.NPY_FR_us
Expand Down
13 changes: 11 additions & 2 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ from pandas._libs.tslibs.conversion cimport (
)
from pandas._libs.tslibs.dtypes cimport (
get_supported_reso,
is_supported_unit,
npy_unit_to_abbrev,
)
from pandas._libs.tslibs.nattype cimport (
Expand Down Expand Up @@ -151,10 +152,10 @@ cdef dict timedelta_abbrevs = {

_no_input = object()


# ----------------------------------------------------------------------
# API


@cython.boundscheck(False)
@cython.wraparound(False)
def ints_to_pytimedelta(ndarray m8values, box=False):
Expand Down Expand Up @@ -1789,7 +1790,6 @@ class Timedelta(_Timedelta):
+ int(kwargs.get("milliseconds", 0) * 1_000_000)
+ seconds
)

if unit in {"Y", "y", "M"}:
raise ValueError(
"Units 'M', 'Y', and 'y' are no longer supported, as they do not "
Expand Down Expand Up @@ -1836,6 +1836,15 @@ class Timedelta(_Timedelta):
return NaT

reso = get_datetime64_unit(value)
if not (is_supported_unit(reso) or
reso in [NPY_DATETIMEUNIT.NPY_FR_m,
NPY_DATETIMEUNIT.NPY_FR_h,
NPY_DATETIMEUNIT.NPY_FR_D,
NPY_DATETIMEUNIT.NPY_FR_W,
NPY_DATETIMEUNIT.NPY_FR_GENERIC]):
err = npy_unit_to_abbrev(reso)
raise ValueError(f" cannot construct a Timedelta from a unit {err}")

new_reso = get_supported_reso(reso)
if reso != NPY_DATETIMEUNIT.NPY_FR_GENERIC:
try:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/libs/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_fast_multiget_timedelta_resos(self):
tm.assert_numpy_array_equal(result, expected)

# case that can't be cast to td64ns
td = Timedelta(np.timedelta64(400, "Y"))
td = Timedelta(np.timedelta64(146000, "D"))
assert hash(td) == hash(td.as_unit("ms"))
assert hash(td) == hash(td.as_unit("us"))
mapping1 = {td: 1}
Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/scalar/timedelta/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ def test_overflow_on_construction():
@pytest.mark.parametrize(
"val, unit",
[
(3508, "M"),
(15251, "W"), # 1
(106752, "D"), # change from previous:
(2562048, "h"), # 0 hours
Expand Down Expand Up @@ -333,7 +332,6 @@ def test_construction_out_of_bounds_td64ns(val, unit):
@pytest.mark.parametrize(
"val, unit",
[
(3508 * 10**9, "M"),
(15251 * 10**9, "W"),
(106752 * 10**9, "D"),
(2562048 * 10**9, "h"),
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/tslibs/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ def test_delta_to_nanoseconds_td64_MY_raises():
delta_to_nanoseconds(td)


@pytest.mark.parametrize("unit", ["Y", "M"])
def test_unsupported_td64_unit_raises(unit):
# GH 52806
with pytest.raises(
ValueError, match=f"cannot construct a Timedelta from a unit {unit}"
):
Timedelta(np.timedelta64(1, unit))


def test_huge_nanoseconds_overflow():
# GH 32402
assert delta_to_nanoseconds(Timedelta(1e10)) == 1e10
Expand Down