diff --git a/pandas/_libs/tslibs/conversion.pxd b/pandas/_libs/tslibs/conversion.pxd index d4ae3fa8c5b99..c74307a3d2887 100644 --- a/pandas/_libs/tslibs/conversion.pxd +++ b/pandas/_libs/tslibs/conversion.pxd @@ -23,6 +23,4 @@ cdef _TSObject convert_datetime_to_tsobject(datetime ts, object tz, cdef int64_t get_datetime64_nanos(object val) except? -1 -cpdef int64_t pydt_to_i8(object pydt) except? -1 - cpdef datetime localize_pydatetime(datetime dt, object tz) diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 77f46016ee846..e0862b9250045 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -223,27 +223,6 @@ cdef class _TSObject: return self.value -cpdef int64_t pydt_to_i8(object pydt) except? -1: - """ - Convert to int64 representation compatible with numpy datetime64; converts - to UTC - - Parameters - ---------- - pydt : object - - Returns - ------- - i8value : np.int64 - """ - cdef: - _TSObject ts - - ts = convert_to_tsobject(pydt, None, None, 0, 0) - - return ts.value - - cdef convert_to_tsobject(object ts, object tz, object unit, bint dayfirst, bint yearfirst, int32_t nanos=0): """ diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 31dc2945f0395..48a3886c20a3a 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -22,7 +22,10 @@ from pandas._libs.tslibs.util cimport is_integer_object from pandas._libs.tslibs.ccalendar import MONTHS, DAYS from pandas._libs.tslibs.ccalendar cimport get_days_in_month, dayofweek -from pandas._libs.tslibs.conversion cimport pydt_to_i8, localize_pydatetime +from pandas._libs.tslibs.conversion cimport ( + convert_datetime_to_tsobject, + localize_pydatetime, +) from pandas._libs.tslibs.nattype cimport NPY_NAT from pandas._libs.tslibs.np_datetime cimport ( npy_datetimestruct, dtstruct_to_dt64, dt64_to_dtstruct) @@ -233,7 +236,10 @@ def _to_dt64D(dt): # numpy.datetime64('2013-05-01T02:00:00.000000+0200') # Thus astype is needed to cast datetime to datetime64[D] if getattr(dt, 'tzinfo', None) is not None: - i8 = pydt_to_i8(dt) + # Get the nanosecond timestamp, + # equiv `Timestamp(dt).value` or `dt.timestamp() * 10**9` + nanos = getattr(dt, "nanosecond", 0) + i8 = convert_datetime_to_tsobject(dt, tz=None, nanos=nanos).value dt = tz_convert_single(i8, UTC, dt.tzinfo) dt = np.int64(dt).astype('datetime64[ns]') else: diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index c60406fdbc8a6..410e449656dbc 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -12,7 +12,6 @@ import pytz from pytz import timezone, utc -from pandas._libs.tslibs import conversion from pandas._libs.tslibs.timezones import dateutil_gettz as gettz, get_timezone import pandas.compat as compat from pandas.compat.numpy import np_datetime64_compat @@ -241,24 +240,20 @@ def test_constructor(self): for result in [Timestamp(date_str), Timestamp(date)]: # only with timestring assert result.value == expected - assert conversion.pydt_to_i8(result) == expected # re-creation shouldn't affect to internal value result = Timestamp(result) assert result.value == expected - assert conversion.pydt_to_i8(result) == expected # with timezone for tz, offset in timezones: for result in [Timestamp(date_str, tz=tz), Timestamp(date, tz=tz)]: expected_tz = expected - offset * 3600 * 1_000_000_000 assert result.value == expected_tz - assert conversion.pydt_to_i8(result) == expected_tz # should preserve tz result = Timestamp(result) assert result.value == expected_tz - assert conversion.pydt_to_i8(result) == expected_tz # should convert to UTC if tz is not None: @@ -267,7 +262,6 @@ def test_constructor(self): result = Timestamp(result, tz="UTC") expected_utc = expected - offset * 3600 * 1_000_000_000 assert result.value == expected_utc - assert conversion.pydt_to_i8(result) == expected_utc def test_constructor_with_stringoffset(self): # GH 7833 @@ -300,30 +294,25 @@ def test_constructor_with_stringoffset(self): for result in [Timestamp(date_str)]: # only with timestring assert result.value == expected - assert conversion.pydt_to_i8(result) == expected # re-creation shouldn't affect to internal value result = Timestamp(result) assert result.value == expected - assert conversion.pydt_to_i8(result) == expected # with timezone for tz, offset in timezones: result = Timestamp(date_str, tz=tz) expected_tz = expected assert result.value == expected_tz - assert conversion.pydt_to_i8(result) == expected_tz # should preserve tz result = Timestamp(result) assert result.value == expected_tz - assert conversion.pydt_to_i8(result) == expected_tz # should convert to UTC result = Timestamp(result).tz_convert("UTC") expected_utc = expected assert result.value == expected_utc - assert conversion.pydt_to_i8(result) == expected_utc # This should be 2013-11-01 05:00 in UTC # converted to Chicago tz