Skip to content

Commit 181f972

Browse files
ArtificialQualiajreback
authored andcommitted
BUG: Fix Timestamp type checks to work with subclassed datetime (#25851) (#25853)
1 parent fa9c7de commit 181f972

15 files changed

+541
-377
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ Sparse
406406
Other
407407
^^^^^
408408

409+
- Improved :class:`Timestamp` type checking in various datetime functions to prevent exceptions when using a subclassed `datetime` (:issue:`25851`)
409410
- Bug in :class:`Series` and :class:`DataFrame` repr where ``np.datetime64('NaT')`` and ``np.timedelta64('NaT')`` with ``dtype=object`` would be represented as ``NaN`` (:issue:`25445`)
410411
-
411412
-

pandas/_libs/tslib.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import cython
33

44
from cpython.datetime cimport (PyDateTime_Check, PyDate_Check,
5-
PyDateTime_CheckExact,
65
PyDateTime_IMPORT,
76
timedelta, datetime, date, time)
87
# import datetime C API
@@ -19,6 +18,7 @@ import pytz
1918
from pandas._libs.util cimport (
2019
is_integer_object, is_float_object, is_datetime64_object)
2120

21+
from pandas._libs.tslibs.c_timestamp cimport _Timestamp
2222

2323
from pandas._libs.tslibs.np_datetime cimport (
2424
check_dts_bounds, npy_datetimestruct, _string_to_dts, dt64_to_dtstruct,
@@ -539,8 +539,7 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
539539
'datetime64 unless utc=True')
540540
else:
541541
iresult[i] = pydatetime_to_dt64(val, &dts)
542-
if not PyDateTime_CheckExact(val):
543-
# i.e. a Timestamp object
542+
if isinstance(val, _Timestamp):
544543
iresult[i] += val.nanosecond
545544
check_dts_bounds(&dts)
546545

pandas/_libs/tslibs/c_timestamp.pxd

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from cpython.datetime cimport datetime
4+
5+
from numpy cimport int64_t
6+
7+
cdef class _Timestamp(datetime):
8+
cdef readonly:
9+
int64_t value, nanosecond
10+
object freq
11+
list _date_attributes
12+
cpdef bint _get_start_end_field(self, str field)
13+
cpdef _get_date_name_field(self, object field, object locale)
14+
cdef int64_t _maybe_convert_value_to_local(self)
15+
cpdef to_datetime64(self)
16+
cdef _assert_tzawareness_compat(_Timestamp self, datetime other)
17+
cpdef datetime to_pydatetime(_Timestamp self, bint warn=*)
18+
cdef bint _compare_outside_nanorange(_Timestamp self, datetime other,
19+
int op) except -1

0 commit comments

Comments
 (0)