Skip to content

PERF: operations with zoneinfo tzinfos #47767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,7 @@ cdef int64_t _tz_localize_using_tzinfo_api(
if not to_utc:
# tz.utcoffset only makes sense if datetime
# is _wall time_, so if val is a UTC timestamp convert to wall time
dt = datetime_new(dts.year, dts.month, dts.day, dts.hour,
dts.min, dts.sec, dts.us, utc_pytz)
dt = dt.astimezone(tz)
dt = _astimezone(dts, tz)

if fold is not NULL:
# NB: fold is only passed with to_utc=False
Expand All @@ -658,6 +656,27 @@ cdef int64_t _tz_localize_using_tzinfo_api(
return delta


cdef datetime _astimezone(npy_datetimestruct dts, tzinfo tz):
"""
Optimized equivalent to:

dt = datetime(dts.year, dts.month, dts.day, dts.hour,
dts.min, dts.sec, dts.us, utc_pytz)
dt = dt.astimezone(tz)

Derived from the datetime.astimezone implementation at
https://github.com/python/cpython/blob/main/Modules/_datetimemodule.c#L6187

NB: we are assuming tz is not None.
"""
cdef:
datetime result

result = datetime_new(dts.year, dts.month, dts.day, dts.hour,
dts.min, dts.sec, dts.us, tz)
return tz.fromutc(result)


# NB: relies on dateutil internals, subject to change.
@cython.boundscheck(False)
@cython.wraparound(False)
Expand Down