Skip to content

Commit 955b35f

Browse files
committed
BUG: fix Timestamp constructor value change on DST Timestamp
1 parent 13b22fd commit 955b35f

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

pandas/_libs/tslibs/conversion.pyx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ cimport numpy as cnp
55
from numpy cimport int64_t, int32_t, intp_t, ndarray
66
cnp.import_array()
77

8+
from dateutil import zoneinfo
9+
810
import pytz
911

1012
# stdlib datetime imports
@@ -382,6 +384,11 @@ cdef _TSObject convert_datetime_to_tsobject(datetime ts, object tz,
382384
obj.tzinfo = tz
383385
else:
384386
obj.value = pydatetime_to_dt64(ts, &obj.dts)
387+
# GH 24329 Take DST offset into account
388+
if isinstance(ts.tzinfo, zoneinfo.tzfile):
389+
if ts.tzinfo.is_ambiguous(ts):
390+
dst_offset = ts.tzinfo.dst(ts)
391+
obj.value += int(dst_offset.total_seconds() * 1e9)
385392
obj.tzinfo = ts.tzinfo
386393

387394
if obj.tzinfo is not None and not is_utc(obj.tzinfo):

pandas/tests/indexes/datetimes/test_timezones.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,7 @@ def test_dti_construction_ambiguous_endpoint(self, tz):
573573
"2013-10-26 23:00", "2013-10-27 01:00", freq="H", tz=tz, ambiguous="infer"
574574
)
575575
assert times[0] == Timestamp("2013-10-26 23:00", tz=tz, freq="H")
576-
577-
if str(tz).startswith("dateutil"):
578-
# fixed ambiguous behavior
579-
# see GH#14621
580-
assert times[-1] == Timestamp("2013-10-27 01:00:00+0100", tz=tz, freq="H")
581-
else:
582-
assert times[-1] == Timestamp("2013-10-27 01:00:00+0000", tz=tz, freq="H")
576+
assert times[-1] == Timestamp("2013-10-27 01:00:00+0000", tz=tz, freq="H")
583577

584578
@pytest.mark.parametrize(
585579
"tz, option, expected",

pandas/tests/scalar/timestamp/test_timestamp.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,3 +1081,13 @@ def test_dt_subclass_add_timedelta(lh, rh):
10811081
result = lh + rh
10821082
expected = SubDatetime(2000, 1, 1, 1)
10831083
assert result == expected
1084+
1085+
def test_constructor_ambigous_dst():
1086+
# GH 24329
1087+
# Make sure that calling Timestamp constructor
1088+
# on Timestamp created from ambiguous time
1089+
# doesn't change Timestamp.value
1090+
ts = Timestamp(1382835600000000000, tz='dateutil/Europe/London')
1091+
expected = ts.value
1092+
result = Timestamp(ts).value
1093+
assert result == expected

0 commit comments

Comments
 (0)