Skip to content

Commit 7412c5a

Browse files
authored
PERF: tz_convert/tz_convert_single (pandas-dev#35087)
1 parent 997c914 commit 7412c5a

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

pandas/_libs/tslibs/tzconversion.pyx

+24-24
Original file line numberDiff line numberDiff line change
@@ -345,36 +345,28 @@ cpdef int64_t tz_convert_single(int64_t val, tzinfo tz1, tzinfo tz2):
345345
converted: int64
346346
"""
347347
cdef:
348-
int64_t utc_date
349348
int64_t arr[1]
349+
bint to_utc = is_utc(tz2)
350+
tzinfo tz
350351

351352
# See GH#17734 We should always be converting either from UTC or to UTC
352-
assert is_utc(tz1) or is_utc(tz2)
353+
assert is_utc(tz1) or to_utc
353354

354355
if val == NPY_NAT:
355356
return val
356357

357-
# Convert to UTC
358-
if is_tzlocal(tz1):
359-
utc_date = _tz_convert_tzlocal_utc(val, tz1, to_utc=True)
360-
elif not is_utc(tz1):
361-
arr[0] = val
362-
utc_date = _tz_convert_dst(arr, tz1, to_utc=True)[0]
358+
if to_utc:
359+
tz = tz1
363360
else:
364-
utc_date = val
361+
tz = tz2
365362

366-
if is_utc(tz2):
367-
return utc_date
368-
elif is_tzlocal(tz2):
369-
return _tz_convert_tzlocal_utc(utc_date, tz2, to_utc=False)
363+
if is_utc(tz):
364+
return val
365+
elif is_tzlocal(tz):
366+
return _tz_convert_tzlocal_utc(val, tz, to_utc=to_utc)
370367
else:
371-
# Convert UTC to other timezone
372-
arr[0] = utc_date
373-
# Note: at least with cython 0.28.3, doing a lookup `[0]` in the next
374-
# line is sensitive to the declared return type of _tz_convert_dst;
375-
# if it is declared as returning ndarray[int64_t], a compile-time error
376-
# is raised.
377-
return _tz_convert_dst(arr, tz2, to_utc=False)[0]
368+
arr[0] = val
369+
return _tz_convert_dst(arr, tz, to_utc=to_utc)[0]
378370

379371

380372
def tz_convert(int64_t[:] vals, tzinfo tz1, tzinfo tz2):
@@ -392,14 +384,22 @@ def tz_convert(int64_t[:] vals, tzinfo tz1, tzinfo tz2):
392384
int64 ndarray of converted
393385
"""
394386
cdef:
395-
int64_t[:] utc_dates, converted
387+
int64_t[:] converted
388+
bint to_utc = is_utc(tz2)
389+
tzinfo tz
390+
391+
# See GH#17734 We should always be converting either from UTC or to UTC
392+
assert is_utc(tz1) or to_utc
396393

397394
if len(vals) == 0:
398395
return np.array([], dtype=np.int64)
399396

400-
# Convert to UTC
401-
utc_dates = _tz_convert_one_way(vals, tz1, to_utc=True)
402-
converted = _tz_convert_one_way(utc_dates, tz2, to_utc=False)
397+
if to_utc:
398+
tz = tz1
399+
else:
400+
tz = tz2
401+
402+
converted = _tz_convert_one_way(vals, tz, to_utc=to_utc)
403403
return np.array(converted, dtype=np.int64)
404404

405405

pandas/tests/tslibs/test_conversion.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ def test_tz_convert_single_matches_tz_convert(tz_aware_fixture, freq):
5757
],
5858
)
5959
def test_tz_convert_corner(arr):
60-
result = tzconversion.tz_convert(
61-
arr, timezones.maybe_get_tz("US/Eastern"), timezones.maybe_get_tz("Asia/Tokyo")
62-
)
60+
result = tzconversion.tz_convert(arr, timezones.maybe_get_tz("US/Eastern"), UTC)
61+
tm.assert_numpy_array_equal(result, arr)
62+
63+
result = tzconversion.tz_convert(arr, UTC, timezones.maybe_get_tz("Asia/Tokyo"))
6364
tm.assert_numpy_array_equal(result, arr)
6465

6566

0 commit comments

Comments
 (0)