Skip to content

BUG: Timedelta(td64, unit=foo) silent overflow #46827

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
Apr 22, 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ Datetimelike
Timedelta
^^^^^^^^^
- Bug in :func:`astype_nansafe` astype("timedelta64[ns]") fails when np.nan is included (:issue:`45798`)
- Bug in constructing a :class:`Timedelta` with a ``np.timedelta64`` object and a ``unit`` sometimes silently overflowing and returning incorrect results instead of raising ``OutOfBoundsTimedelta`` (:issue:`46827`)
-

Time Zones
^^^^^^^^^^
Expand Down
10 changes: 10 additions & 0 deletions pandas/_libs/tslibs/dtypes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ cdef str npy_unit_to_abbrev(NPY_DATETIMEUNIT unit):
return "M"
elif unit == NPY_DATETIMEUNIT.NPY_FR_Y:
return "Y"

# Checks for not-really-supported units go at the end, as we don't expect
# to see these often
elif unit == NPY_DATETIMEUNIT.NPY_FR_ps:
return "ps"
elif unit == NPY_DATETIMEUNIT.NPY_FR_fs:
return "fs"
elif unit == NPY_DATETIMEUNIT.NPY_FR_as:
return "as"

else:
raise NotImplementedError(unit)

Expand Down
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1487,8 +1487,6 @@ class Timedelta(_Timedelta):
elif PyDelta_Check(value):
value = convert_to_timedelta64(value, 'ns')
elif is_timedelta64_object(value):
if unit is not None:
value = value.astype(f'timedelta64[{unit}]')
value = ensure_td64ns(value)
elif is_tick_object(value):
value = np.timedelta64(value.nanos, 'ns')
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/scalar/timedelta/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@
)


def test_construct_from_td64_with_unit():
# ignore the unit, as it may cause silently overflows leading to incorrect
# results, and in non-overflow cases is irrelevant GH#46827
obj = np.timedelta64(123456789, "h")

with pytest.raises(OutOfBoundsTimedelta, match="123456789 hours"):
Timedelta(obj, unit="ps")

with pytest.raises(OutOfBoundsTimedelta, match="123456789 hours"):
Timedelta(obj, unit="ns")

with pytest.raises(OutOfBoundsTimedelta, match="123456789 hours"):
Timedelta(obj)


def test_construction():
expected = np.timedelta64(10, "D").astype("m8[ns]").view("i8")
assert Timedelta(10, unit="d").value == expected
Expand Down