Skip to content

BUG: disallow exotic np.datetime64 unit #61882

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 5 commits into from
Jul 17, 2025
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ Datetimelike
- Bug in :attr:`is_year_start` where a DateTimeIndex constructed via a date_range with frequency 'MS' wouldn't have the correct year or quarter start attributes (:issue:`57377`)
- Bug in :class:`DataFrame` raising ``ValueError`` when ``dtype`` is ``timedelta64`` and ``data`` is a list containing ``None`` (:issue:`60064`)
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)
- Bug in :class:`Timestamp` constructor failing to raise when given a ``np.datetime64`` object with non-standard unit (:issue:`25611`)
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56147`)
- Bug in :func:`tseries.api.guess_datetime_format` would fail to infer time format when "%Y" == "%H%M" (:issue:`57452`)
Expand Down
9 changes: 9 additions & 0 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import numpy as np
cimport numpy as cnp
from libc.math cimport log10
from numpy cimport (
PyDatetimeScalarObject,
float64_t,
int32_t,
int64_t,
Expand Down Expand Up @@ -358,6 +359,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
cdef:
_TSObject obj
NPY_DATETIMEUNIT reso
int64_t num

obj = _TSObject()

Expand All @@ -367,6 +369,13 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
if checknull_with_nat_and_na(ts):
obj.value = NPY_NAT
elif cnp.is_datetime64_object(ts):
num = (<PyDatetimeScalarObject*>ts).obmeta.num
if num != 1:
raise ValueError(
# GH#25611
"np.datetime64 objects with units containing a multiplier are "
"not supported"
)
reso = get_supported_reso(get_datetime64_unit(ts))
obj.creso = reso
obj.value = get_datetime64_nanos(ts, reso)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/scalar/timestamp/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,13 @@ def test_now_today_unit(self, method):


class TestTimestampConstructors:
def test_disallow_dt64_with_weird_unit(self):
# GH#25611
dt64 = np.datetime64(1, "500m")
msg = "np.datetime64 objects with units containing a multiplier"
with pytest.raises(ValueError, match=msg):
Timestamp(dt64)

def test_weekday_but_no_day_raises(self):
# GH#52659
msg = "Parsing datetimes with weekday but no day information is not supported"
Expand Down
Loading