Skip to content

Commit

Permalink
BUG: Cannot use tz-aware origin in to_datetime (#16842)
Browse files Browse the repository at this point in the history
  • Loading branch information
injinbae committed Aug 14, 2017
1 parent 330b8c1 commit 5ab75f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
6 changes: 5 additions & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/tseries/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 5ab75f0

Please sign in to comment.