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
ivybae committed Aug 15, 2017
1 parent 330b8c1 commit a199cb3
Show file tree
Hide file tree
Showing 3 changed files with 14 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 kwarg (: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
8 changes: 7 additions & 1 deletion pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import dateutil
import numpy as np
from dateutil.parser import parse
from datetime import datetime, date, time
from datetime import datetime, date, time, timezone
from distutils.version import LooseVersion

import pandas as pd
Expand Down 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=timezone.utc))

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

0 comments on commit a199cb3

Please sign in to comment.