Skip to content

Commit 4d10233

Browse files
authored
PERF: fastpaths in tzconversion (#51773)
1 parent 376f77d commit 4d10233

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

pandas/_libs/tslibs/tzconversion.pyx

+5-2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ timedelta-like}
224224
-------
225225
localized : ndarray[int64_t]
226226
"""
227+
228+
if tz is None or is_utc(tz) or vals.size == 0:
229+
# Fastpath, avoid overhead of creating Localizer
230+
return vals.copy()
231+
227232
cdef:
228233
ndarray[uint8_t, cast=True] ambiguous_array
229234
Py_ssize_t i, n = vals.shape[0]
@@ -244,8 +249,6 @@ timedelta-like}
244249
npy_datetimestruct dts
245250

246251
# Vectorized version of DstTzInfo.localize
247-
if info.use_utc:
248-
return vals.copy()
249252

250253
# silence false-positive compiler warning
251254
ambiguous_array = np.empty(0, dtype=bool)

pandas/_libs/tslibs/vectorized.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ def tz_convert_from_utc(ndarray stamps, tzinfo tz, NPY_DATETIMEUNIT reso=NPY_FR_
5454
-------
5555
ndarray[int64]
5656
"""
57+
if tz is None or is_utc(tz) or stamps.size == 0:
58+
# Much faster than going through the "standard" pattern below;
59+
# do this before initializing Localizer.
60+
return stamps.copy()
61+
5762
cdef:
5863
Localizer info = Localizer(tz, creso=reso)
5964
int64_t utc_val, local_val
@@ -62,10 +67,6 @@ def tz_convert_from_utc(ndarray stamps, tzinfo tz, NPY_DATETIMEUNIT reso=NPY_FR_
6267
ndarray result
6368
cnp.broadcast mi
6469

65-
if tz is None or is_utc(tz) or stamps.size == 0:
66-
# Much faster than going through the "standard" pattern below
67-
return stamps.copy()
68-
6970
result = cnp.PyArray_EMPTY(stamps.ndim, stamps.shape, cnp.NPY_INT64, 0)
7071
mi = cnp.PyArray_MultiIterNew2(result, stamps)
7172

0 commit comments

Comments
 (0)