Skip to content

Commit c4f729e

Browse files
author
MarcoGorelli
committed
wip
1 parent 6b0ac93 commit c4f729e

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

pandas/_libs/tslib.pyx

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ from cpython.object cimport PyObject
1212
import_datetime()
1313

1414

15+
import dateutil
1516
cimport numpy as cnp
1617
from numpy cimport (
1718
float64_t,
@@ -516,6 +517,7 @@ cpdef array_to_datetime(
516517
assert is_raise or is_ignore or is_coerce
517518

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

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

641645
_ts = convert_datetime_to_tsobject(py_dt, None)
642646
iresult[i] = _ts.value
@@ -650,6 +654,7 @@ cpdef array_to_datetime(
650654
# since we store the total_seconds of
651655
# dateutil.tz.tzoffset objects
652656
out_tzoffset_vals.add(out_tzoffset * 60.)
657+
result_timezone[i] = out_tzoffset * 60.
653658
tz = pytz.FixedOffset(out_tzoffset)
654659
value = tz_localize_to_utc_single(value, tz)
655660
out_local = 0
@@ -658,6 +663,7 @@ cpdef array_to_datetime(
658663
# Add a marker for naive string, to track if we are
659664
# parsing mixed naive and aware strings
660665
out_tzoffset_vals.add("naive")
666+
result_timezone[i] = None
661667
iresult[i] = value
662668
check_dts_bounds(&dts)
663669

@@ -715,7 +721,18 @@ cpdef array_to_datetime(
715721
# (with individual dateutil.tzoffsets) are returned
716722
is_same_offsets = len(out_tzoffset_vals) == 1
717723
if not is_same_offsets:
718-
return _array_to_datetime_object(values, errors, dayfirst, yearfirst)
724+
_result = np.empty(n, dtype="object")
725+
for i in range(n):
726+
if iresult[i] == NPY_NAT:
727+
_result[i] = NaT
728+
continue
729+
_dt = parse_datetime_string(str(result[i]))
730+
if result_timezone[i] is not None:
731+
_tzinfo = dateutil.tz.tzoffset(None, result_timezone[i])
732+
_result[i] = _dt.replace(tzinfo=pytz.UTC).astimezone(_tzinfo)
733+
else:
734+
_result[i] = _dt
735+
return _result, None
719736
else:
720737
tz_offset = out_tzoffset_vals.pop()
721738
tz_out = pytz.FixedOffset(tz_offset / 60.)

pandas/tests/tools/test_to_datetime.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1353,11 +1353,11 @@ def test_mixed_offsets_with_native_datetime_raises(self):
13531353
mixed = to_datetime(ser)
13541354
expected = Series(
13551355
[
1356-
"NaT",
1356+
NaT,
13571357
Timestamp("1990-01-01"),
13581358
Timestamp("2015-03-14T16:15:14.123-08:00").to_pydatetime(),
13591359
Timestamp("2019-03-04T21:56:32.620-07:00").to_pydatetime(),
1360-
None,
1360+
NaT,
13611361
],
13621362
dtype=object,
13631363
)

0 commit comments

Comments
 (0)