From 5ab75f0434a65a70d84ef693a91dd0d3d8c855dc Mon Sep 17 00:00:00 2001 From: injinbae Date: Mon, 14 Aug 2017 16:41:01 +0900 Subject: [PATCH] BUG: Cannot use tz-aware origin in to_datetime (#16842) --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/tools/datetimes.py | 6 +++++- pandas/tests/tseries/test_timezones.py | 8 +++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 8b2c4d16f4e1a0..693e623f104c11 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -385,3 +385,4 @@ Other - Bug in :func:`eval` where the ``inplace`` parameter was being incorrectly handled (:issue:`16732`) - Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`) - Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` (:issue:`17108`) +- Bug in :func:`to_datetime` that cannot use tz-aware origin (:issue:`16842`) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 6ff4302937d073..5cf3734db6ec7a 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -488,7 +488,11 @@ def _convert_listlike(arg, box, format, name=None, tz=tz): # we are going to offset back to unix / epoch time try: - offset = tslib.Timestamp(origin) - tslib.Timestamp(0) + origin_ts = tslib.Timestamp(origin) + epoch_ts = tslib.Timestamp(0) + if origin_ts.tzinfo is not None: + epoch_ts = epoch_ts.replace(tzinfo = origin_ts.tzinfo) + offset = origin_ts - epoch_ts except tslib.OutOfBoundsDatetime: raise tslib.OutOfBoundsDatetime( "origin {} is Out of Bounds".format(origin)) diff --git a/pandas/tests/tseries/test_timezones.py b/pandas/tests/tseries/test_timezones.py index a9ecfd797a32bc..d455fe6eb528d6 100644 --- a/pandas/tests/tseries/test_timezones.py +++ b/pandas/tests/tseries/test_timezones.py @@ -9,7 +9,7 @@ from pytz import NonExistentTimeError from distutils.version import LooseVersion from dateutil.tz import tzlocal, tzoffset -from datetime import datetime, timedelta, tzinfo, date +from datetime import datetime, timedelta, tzinfo, date, timezone import pandas.util.testing as tm import pandas.core.tools.datetimes as tools @@ -204,6 +204,12 @@ def test_timestamp_to_datetime_tzoffset(self): result = Timestamp(expected.to_pydatetime()) assert expected == result + def test_timestamp_to_datetime_tzinfo(self): + # GH16842 + expected = Timestamp('2000-01-02 00:00:00') + result = to_datetime(1, unit='D', origin=datetime(2000, 1, 1, tzinfo=timezone.utc)) + assert expected == result + def test_timedelta_push_over_dst_boundary(self): # #1389