Skip to content

Commit 651a55f

Browse files
committed
BUG: nonexistent Timestamp pre-summer/winter DST change with dateutil timezone
1 parent 3fbc332 commit 651a55f

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ Categorical
5959

6060
Datetimelike
6161
^^^^^^^^^^^^
62+
6263
- Bug in :class:`Timestamp` where constructing :class:`Timestamp` from ambiguous epoch time and calling constructor again changed :meth:`Timestamp.value` property (:issue:`24329`)
6364
- :meth:`DatetimeArray.searchsorted`, :meth:`TimedeltaArray.searchsorted`, :meth:`PeriodArray.searchsorted` not recognizing non-pandas scalars and incorrectly raising ``ValueError`` instead of ``TypeError`` (:issue:`30950`)
64-
-
65+
- Bug in :class:`Timestamp` where constructing :class:`Timestamp` with dateutil timezone less than 128 nanoseconds before daylight saving time switch from winter to summer would result in nonexistent time (:issue:`31043`)
6566

6667
Timedelta
6768
^^^^^^^^^

pandas/_libs/tslibs/nattype.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ cdef class _NaT(datetime):
278278

279279
def total_seconds(self):
280280
"""
281-
Total duration of timedelta in seconds (to ns precision).
281+
Total duration of timedelta in seconds (to microsecond precision).
282282
"""
283283
# GH#10939
284284
return np.nan

pandas/_libs/tslibs/timedeltas.pyx

+4-2
Original file line numberDiff line numberDiff line change
@@ -861,9 +861,11 @@ cdef class _Timedelta(timedelta):
861861

862862
def total_seconds(self):
863863
"""
864-
Total duration of timedelta in seconds (to ns precision).
864+
Total duration of timedelta in seconds (to microsecond precision).
865865
"""
866-
return self.value / 1e9
866+
# GH 31043
867+
# Round down to microseconds to avoid confusing tzinfo.utcoffset
868+
return (self.value - self.value % 1000) / 1e9
867869

868870
def view(self, dtype):
869871
"""

pandas/tests/scalar/timestamp/test_timestamp.py

+12
Original file line numberDiff line numberDiff line change
@@ -1092,3 +1092,15 @@ def test_constructor_ambigous_dst():
10921092
expected = ts.value
10931093
result = Timestamp(ts).value
10941094
assert result == expected
1095+
1096+
1097+
def test_constructor_before_dst_switch():
1098+
# GH 31043
1099+
# Make sure that calling Timestamp constructor
1100+
# on time just before DST switch doesn't lead to
1101+
# nonexistent time
1102+
epoch = 1552211999999999872
1103+
ts = Timestamp(epoch, tz="dateutil/US/Pacific")
1104+
expected = timedelta(seconds=0)
1105+
result = ts.tz.dst(ts)
1106+
assert result == expected

0 commit comments

Comments
 (0)