Skip to content

Commit 6fca116

Browse files
authored
BUG: disallow exotic np.datetime64 unit (#61882)
1 parent 6537afe commit 6fca116

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ Datetimelike
694694
- 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`)
695695
- Bug in :class:`DataFrame` raising ``ValueError`` when ``dtype`` is ``timedelta64`` and ``data`` is a list containing ``None`` (:issue:`60064`)
696696
- Bug in :class:`Timestamp` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``tzinfo`` or data (:issue:`48688`)
697+
- Bug in :class:`Timestamp` constructor failing to raise when given a ``np.datetime64`` object with non-standard unit (:issue:`25611`)
697698
- Bug in :func:`date_range` where the last valid timestamp would sometimes not be produced (:issue:`56134`)
698699
- Bug in :func:`date_range` where using a negative frequency value would not include all points between the start and end values (:issue:`56147`)
699700
- Bug in :func:`tseries.api.guess_datetime_format` would fail to infer time format when "%Y" == "%H%M" (:issue:`57452`)

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*>ts).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"
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy