Skip to content

WIP: Dont double parse #50073

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

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 18 additions & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ from cpython.object cimport PyObject
import_datetime()


import dateutil
cimport numpy as cnp
from numpy cimport (
float64_t,
Expand Down Expand Up @@ -516,6 +517,7 @@ cpdef array_to_datetime(
assert is_raise or is_ignore or is_coerce

result = np.empty(n, dtype="M8[ns]")
result_timezone = np.empty(n, dtype="object")
iresult = result.view("i8")

try:
Expand Down Expand Up @@ -633,10 +635,12 @@ cpdef array_to_datetime(
# dateutil timezone objects cannot be hashed, so
# store the UTC offsets in seconds instead
out_tzoffset_vals.add(tz.total_seconds())
result_timezone[i] = tz.total_seconds()
else:
# Add a marker for naive string, to track if we are
# parsing mixed naive and aware strings
out_tzoffset_vals.add("naive")
result_timezone[i] = None

_ts = convert_datetime_to_tsobject(py_dt, None)
iresult[i] = _ts.value
Expand All @@ -650,6 +654,7 @@ cpdef array_to_datetime(
# since we store the total_seconds of
# dateutil.tz.tzoffset objects
out_tzoffset_vals.add(out_tzoffset * 60.)
result_timezone[i] = out_tzoffset * 60.
tz = pytz.FixedOffset(out_tzoffset)
value = tz_localize_to_utc_single(value, tz)
out_local = 0
Expand All @@ -658,6 +663,7 @@ cpdef array_to_datetime(
# Add a marker for naive string, to track if we are
# parsing mixed naive and aware strings
out_tzoffset_vals.add("naive")
result_timezone[i] = None
iresult[i] = value
check_dts_bounds(&dts)

Expand Down Expand Up @@ -715,7 +721,18 @@ cpdef array_to_datetime(
# (with individual dateutil.tzoffsets) are returned
is_same_offsets = len(out_tzoffset_vals) == 1
if not is_same_offsets:
return _array_to_datetime_object(values, errors, dayfirst, yearfirst)
_result = np.empty(n, dtype="object")
for i in range(n):
if iresult[i] == NPY_NAT:
_result[i] = NaT
continue
_dt = parse_datetime_string(str(result[i]))
if result_timezone[i] is not None:
_tzinfo = dateutil.tz.tzoffset(None, result_timezone[i])
_result[i] = _dt.replace(tzinfo=pytz.UTC).astimezone(_tzinfo)
else:
_result[i] = _dt
return _result, None
else:
tz_offset = out_tzoffset_vals.pop()
tz_out = pytz.FixedOffset(tz_offset / 60.)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/tools/test_to_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,11 +1353,11 @@ def test_mixed_offsets_with_native_datetime_raises(self):
mixed = to_datetime(ser)
expected = Series(
[
"NaT",
NaT,
Timestamp("1990-01-01"),
Timestamp("2015-03-14T16:15:14.123-08:00").to_pydatetime(),
Timestamp("2019-03-04T21:56:32.620-07:00").to_pydatetime(),
None,
NaT,
],
dtype=object,
)
Expand Down