Skip to content

Commit eb23512

Browse files
authored
PERF: _return_parsed_timezone_results (pandas-dev#50168)
* PERF: _return_parsed_timezone_results * whatsnew * fix * whatsnew
1 parent 5fe95c6 commit eb23512

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ Performance improvements
662662
- Performance improvement for :class:`Series` constructor passing integer numpy array with nullable dtype (:issue:`48338`)
663663
- Performance improvement for :class:`DatetimeIndex` constructor passing a list (:issue:`48609`)
664664
- Performance improvement in :func:`merge` and :meth:`DataFrame.join` when joining on a sorted :class:`MultiIndex` (:issue:`48504`)
665+
- Performance improvement in :func:`to_datetime` when parsing strings with timezone offsets (:issue:`50107`)
665666
- Performance improvement in :meth:`DataFrame.loc` and :meth:`Series.loc` for tuple-based indexing of a :class:`MultiIndex` (:issue:`48384`)
666667
- Performance improvement for :meth:`MultiIndex.unique` (:issue:`48335`)
667668
- Performance improvement for :func:`concat` with extension array backed indexes (:issue:`49128`, :issue:`49178`)

pandas/core/tools/datetimes.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -313,19 +313,16 @@ def _return_parsed_timezone_results(
313313
-------
314314
tz_result : Index-like of parsed dates with timezone
315315
"""
316-
tz_results = np.array(
317-
[Timestamp(res).tz_localize(zone) for res, zone in zip(result, timezones)]
318-
)
319-
if utc:
320-
# Convert to the same tz
321-
tz_results = np.array(
322-
[
323-
tz_result.tz_convert("utc")
324-
if tz_result.tzinfo is not None
325-
else tz_result.tz_localize("utc")
326-
for tz_result in tz_results
327-
]
328-
)
316+
tz_results = np.empty(len(result), dtype=object)
317+
for zone in unique(timezones):
318+
mask = timezones == zone
319+
dta = DatetimeArray(result[mask]).tz_localize(zone)
320+
if utc:
321+
if dta.tzinfo is None:
322+
dta = dta.tz_localize("utc")
323+
else:
324+
dta = dta.tz_convert("utc")
325+
tz_results[mask] = dta
329326

330327
return Index(tz_results, name=name)
331328

0 commit comments

Comments
 (0)