Skip to content

Commit

Permalink
BUG: Cannot use tz-aware origin in to_datetime (pandas-dev#16842)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivybae committed Aug 21, 2017
1 parent 330b8c1 commit 09d051d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
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`)
- :func:`to_datetime` passed tz-aware timestamp origin kwarg now raises correct ValueError (:issue:`16842`)
7 changes: 6 additions & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,19 @@ 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)
offset = tslib.Timestamp(origin)
except tslib.OutOfBoundsDatetime:
raise tslib.OutOfBoundsDatetime(
"origin {} is Out of Bounds".format(origin))
except ValueError:
raise ValueError("origin {} cannot be converted "
"to a Timestamp".format(origin))

if offset.tz is not None:
raise ValueError(
"offset {} must have no timezone".format(offset))
offset -= tslib.Timestamp(0)

# convert the offset to the unit of the arg
# this should be lossless in terms of precision
offset = offset // tslib.Timedelta(1, unit=unit)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,12 @@ def test_invalid_origins(self, origin, exc, units, units_from_epochs):
pd.to_datetime(units_from_epochs, unit=units,
origin=origin)

def test_invalid_origins_tzinfo(self):
# GH16842
with pytest.raises(ValueError):
pd.to_datetime(1, unit='D',
origin=datetime(2000, 1, 1, tzinfo=pytz.utc))

def test_processing_order(self):
# make sure we handle out-of-bounds *before*
# constructing the dates
Expand Down

0 comments on commit 09d051d

Please sign in to comment.