Skip to content

Commit 5ab75f0

Browse files
committed
BUG: Cannot use tz-aware origin in to_datetime (#16842)
1 parent 330b8c1 commit 5ab75f0

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,4 @@ Other
385385
- Bug in :func:`eval` where the ``inplace`` parameter was being incorrectly handled (:issue:`16732`)
386386
- Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`)
387387
- Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` (:issue:`17108`)
388+
- Bug in :func:`to_datetime` that cannot use tz-aware origin (:issue:`16842`)

pandas/core/tools/datetimes.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,11 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
488488

489489
# we are going to offset back to unix / epoch time
490490
try:
491-
offset = tslib.Timestamp(origin) - tslib.Timestamp(0)
491+
origin_ts = tslib.Timestamp(origin)
492+
epoch_ts = tslib.Timestamp(0)
493+
if origin_ts.tzinfo is not None:
494+
epoch_ts = epoch_ts.replace(tzinfo = origin_ts.tzinfo)
495+
offset = origin_ts - epoch_ts
492496
except tslib.OutOfBoundsDatetime:
493497
raise tslib.OutOfBoundsDatetime(
494498
"origin {} is Out of Bounds".format(origin))

pandas/tests/tseries/test_timezones.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pytz import NonExistentTimeError
1010
from distutils.version import LooseVersion
1111
from dateutil.tz import tzlocal, tzoffset
12-
from datetime import datetime, timedelta, tzinfo, date
12+
from datetime import datetime, timedelta, tzinfo, date, timezone
1313

1414
import pandas.util.testing as tm
1515
import pandas.core.tools.datetimes as tools
@@ -204,6 +204,12 @@ def test_timestamp_to_datetime_tzoffset(self):
204204
result = Timestamp(expected.to_pydatetime())
205205
assert expected == result
206206

207+
def test_timestamp_to_datetime_tzinfo(self):
208+
# GH16842
209+
expected = Timestamp('2000-01-02 00:00:00')
210+
result = to_datetime(1, unit='D', origin=datetime(2000, 1, 1, tzinfo=timezone.utc))
211+
assert expected == result
212+
207213
def test_timedelta_push_over_dst_boundary(self):
208214
# #1389
209215

0 commit comments

Comments
 (0)