Skip to content

Commit f84f594

Browse files
author
MarcoGorelli
committed
come on...
1 parent 6b0ac93 commit f84f594

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

pandas/_libs/tslib.pyx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ from pandas._libs.util cimport (
4242
is_float_object,
4343
is_integer_object,
4444
)
45+
import datetime as dt
4546

4647
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
4748
from pandas._libs.tslibs.parsing import parse_datetime_string
@@ -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,23 @@ 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 checknull_with_nat_and_na(iresult[i]):
727+
# GH 25978. No need to parse NaT-like or datetime-like vals
728+
_result[i] = NPY_NAT
729+
continue
730+
_dt = dt.datetime.utcfromtimestamp(iresult[i]/1_000_000_000)
731+
if result_timezone[i] is not None:
732+
_tzinfo = pytz.FixedOffset(result_timezone[i]/60)
733+
_result[i] = _dt.replace(tzinfo=pytz.UTC).astimezone(_tzinfo)
734+
else:
735+
_result[i] = _dt
736+
print("_dt", _dt)
737+
print("resultimteinzei", result_timezone[i])
738+
print("iresulti", iresult[i])
739+
print("resulti", result[i])
740+
return _result, None
719741
else:
720742
tz_offset = out_tzoffset_vals.pop()
721743
tz_out = pytz.FixedOffset(tz_offset / 60.)

0 commit comments

Comments
 (0)