Skip to content

Commit ac01192

Browse files
committed
BUG: disallow exotic np.datetime64 unit
1 parent 1d153bb commit ac01192

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

pandas/_libs/tslibs/conversion.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import numpy as np
55
cimport numpy as cnp
66
from libc.math cimport log10
77
from numpy cimport (
8+
PyDatetimeScalarObject,
89
float64_t,
910
int32_t,
1011
int64_t,
@@ -358,6 +359,7 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
358359
cdef:
359360
_TSObject obj
360361
NPY_DATETIMEUNIT reso
362+
int64_t num
361363

362364
obj = _TSObject()
363365

@@ -367,6 +369,13 @@ cdef _TSObject convert_to_tsobject(object ts, tzinfo tz, str unit,
367369
if checknull_with_nat_and_na(ts):
368370
obj.value = NPY_NAT
369371
elif cnp.is_datetime64_object(ts):
372+
num = (<PyDatetimeScalarObject*>obj).obmeta.num
373+
if num != 1:
374+
raise ValueError(
375+
# GH#25611
376+
"np.datetime64 objects with units containing a multiplier are "
377+
"not supported", num
378+
)
370379
reso = get_supported_reso(get_datetime64_unit(ts))
371380
obj.creso = reso
372381
obj.value = get_datetime64_nanos(ts, reso)

pandas/tests/scalar/timestamp/test_constructors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,13 @@ def test_now_today_unit(self, method):
478478

479479

480480
class TestTimestampConstructors:
481+
def test_disallow_dt64_with_weird_unit(self):
482+
# GH#25611
483+
dt64 = np.datetime64(1, "500m")
484+
msg = "np.datetime64 objects with units containing a multiplier"
485+
with pytest.raises(ValueError, match=msg):
486+
Timestamp(dt64)
487+
481488
def test_weekday_but_no_day_raises(self):
482489
# GH#52659
483490
msg = "Parsing datetimes with weekday but no day information is not supported"

0 commit comments

Comments
 (0)