Skip to content

Commit f6ef10a

Browse files
jbrockmendeljreback
authored andcommitted
CLN: remove pydt_to_i8 (#30854)
1 parent 2dadd0f commit f6ef10a

File tree

4 files changed

+8
-36
lines changed

4 files changed

+8
-36
lines changed

pandas/_libs/tslibs/conversion.pxd

-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@ cdef _TSObject convert_datetime_to_tsobject(datetime ts, object tz,
2323

2424
cdef int64_t get_datetime64_nanos(object val) except? -1
2525

26-
cpdef int64_t pydt_to_i8(object pydt) except? -1
27-
2826
cpdef datetime localize_pydatetime(datetime dt, object tz)

pandas/_libs/tslibs/conversion.pyx

-21
Original file line numberDiff line numberDiff line change
@@ -223,27 +223,6 @@ cdef class _TSObject:
223223
return self.value
224224

225225

226-
cpdef int64_t pydt_to_i8(object pydt) except? -1:
227-
"""
228-
Convert to int64 representation compatible with numpy datetime64; converts
229-
to UTC
230-
231-
Parameters
232-
----------
233-
pydt : object
234-
235-
Returns
236-
-------
237-
i8value : np.int64
238-
"""
239-
cdef:
240-
_TSObject ts
241-
242-
ts = convert_to_tsobject(pydt, None, None, 0, 0)
243-
244-
return ts.value
245-
246-
247226
cdef convert_to_tsobject(object ts, object tz, object unit,
248227
bint dayfirst, bint yearfirst, int32_t nanos=0):
249228
"""

pandas/_libs/tslibs/offsets.pyx

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ from pandas._libs.tslibs.util cimport is_integer_object
2222

2323
from pandas._libs.tslibs.ccalendar import MONTHS, DAYS
2424
from pandas._libs.tslibs.ccalendar cimport get_days_in_month, dayofweek
25-
from pandas._libs.tslibs.conversion cimport pydt_to_i8, localize_pydatetime
25+
from pandas._libs.tslibs.conversion cimport (
26+
convert_datetime_to_tsobject,
27+
localize_pydatetime,
28+
)
2629
from pandas._libs.tslibs.nattype cimport NPY_NAT
2730
from pandas._libs.tslibs.np_datetime cimport (
2831
npy_datetimestruct, dtstruct_to_dt64, dt64_to_dtstruct)
@@ -233,7 +236,10 @@ def _to_dt64D(dt):
233236
# numpy.datetime64('2013-05-01T02:00:00.000000+0200')
234237
# Thus astype is needed to cast datetime to datetime64[D]
235238
if getattr(dt, 'tzinfo', None) is not None:
236-
i8 = pydt_to_i8(dt)
239+
# Get the nanosecond timestamp,
240+
# equiv `Timestamp(dt).value` or `dt.timestamp() * 10**9`
241+
nanos = getattr(dt, "nanosecond", 0)
242+
i8 = convert_datetime_to_tsobject(dt, tz=None, nanos=nanos).value
237243
dt = tz_convert_single(i8, UTC, dt.tzinfo)
238244
dt = np.int64(dt).astype('datetime64[ns]')
239245
else:

pandas/tests/scalar/timestamp/test_timestamp.py

-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import pytz
1414
from pytz import timezone, utc
1515

16-
from pandas._libs.tslibs import conversion
1716
from pandas._libs.tslibs.timezones import dateutil_gettz as gettz, get_timezone
1817
import pandas.compat as compat
1918
from pandas.compat.numpy import np_datetime64_compat
@@ -242,24 +241,20 @@ def test_constructor(self):
242241
for result in [Timestamp(date_str), Timestamp(date)]:
243242
# only with timestring
244243
assert result.value == expected
245-
assert conversion.pydt_to_i8(result) == expected
246244

247245
# re-creation shouldn't affect to internal value
248246
result = Timestamp(result)
249247
assert result.value == expected
250-
assert conversion.pydt_to_i8(result) == expected
251248

252249
# with timezone
253250
for tz, offset in timezones:
254251
for result in [Timestamp(date_str, tz=tz), Timestamp(date, tz=tz)]:
255252
expected_tz = expected - offset * 3600 * 1_000_000_000
256253
assert result.value == expected_tz
257-
assert conversion.pydt_to_i8(result) == expected_tz
258254

259255
# should preserve tz
260256
result = Timestamp(result)
261257
assert result.value == expected_tz
262-
assert conversion.pydt_to_i8(result) == expected_tz
263258

264259
# should convert to UTC
265260
if tz is not None:
@@ -268,7 +263,6 @@ def test_constructor(self):
268263
result = Timestamp(result, tz="UTC")
269264
expected_utc = expected - offset * 3600 * 1_000_000_000
270265
assert result.value == expected_utc
271-
assert conversion.pydt_to_i8(result) == expected_utc
272266

273267
def test_constructor_with_stringoffset(self):
274268
# GH 7833
@@ -301,30 +295,25 @@ def test_constructor_with_stringoffset(self):
301295
for result in [Timestamp(date_str)]:
302296
# only with timestring
303297
assert result.value == expected
304-
assert conversion.pydt_to_i8(result) == expected
305298

306299
# re-creation shouldn't affect to internal value
307300
result = Timestamp(result)
308301
assert result.value == expected
309-
assert conversion.pydt_to_i8(result) == expected
310302

311303
# with timezone
312304
for tz, offset in timezones:
313305
result = Timestamp(date_str, tz=tz)
314306
expected_tz = expected
315307
assert result.value == expected_tz
316-
assert conversion.pydt_to_i8(result) == expected_tz
317308

318309
# should preserve tz
319310
result = Timestamp(result)
320311
assert result.value == expected_tz
321-
assert conversion.pydt_to_i8(result) == expected_tz
322312

323313
# should convert to UTC
324314
result = Timestamp(result).tz_convert("UTC")
325315
expected_utc = expected
326316
assert result.value == expected_utc
327-
assert conversion.pydt_to_i8(result) == expected_utc
328317

329318
# This should be 2013-11-01 05:00 in UTC
330319
# converted to Chicago tz

0 commit comments

Comments
 (0)