Skip to content

PERF: fastpaths in tzconversion #51773

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
Mar 7, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions pandas/_libs/tslibs/tzconversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ timedelta-like}
-------
localized : ndarray[int64_t]
"""

if tz is None or is_utc(tz) or vals.size == 0:
# Fastpath, avoid overhead of creating Localizer
return vals.copy()

cdef:
ndarray[uint8_t, cast=True] ambiguous_array
Py_ssize_t i, n = vals.shape[0]
Expand All @@ -244,8 +249,6 @@ timedelta-like}
npy_datetimestruct dts

# Vectorized version of DstTzInfo.localize
if info.use_utc:
return vals.copy()

# silence false-positive compiler warning
ambiguous_array = np.empty(0, dtype=bool)
Expand Down
9 changes: 5 additions & 4 deletions pandas/_libs/tslibs/vectorized.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def tz_convert_from_utc(ndarray stamps, tzinfo tz, NPY_DATETIMEUNIT reso=NPY_FR_
-------
ndarray[int64]
"""
if tz is None or is_utc(tz) or stamps.size == 0:
# Much faster than going through the "standard" pattern below;
# do this before initializing Localizer.
return stamps.copy()

cdef:
Localizer info = Localizer(tz, creso=reso)
int64_t utc_val, local_val
Expand All @@ -62,10 +67,6 @@ def tz_convert_from_utc(ndarray stamps, tzinfo tz, NPY_DATETIMEUNIT reso=NPY_FR_
ndarray result
cnp.broadcast mi

if tz is None or is_utc(tz) or stamps.size == 0:
# Much faster than going through the "standard" pattern below
return stamps.copy()

result = cnp.PyArray_EMPTY(stamps.ndim, stamps.shape, cnp.NPY_INT64, 0)
mi = cnp.PyArray_MultiIterNew2(result, stamps)

Expand Down