Skip to content

Commit daff5b4

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

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
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

+4
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
486486
arg=arg,
487487
origin=origin))
488488

489+
if origin.tzinfo is not None:
490+
raise ValueError(
491+
"origin {} must have no timezone".format(origin))
492+
489493
# we are going to offset back to unix / epoch time
490494
try:
491495
offset = tslib.Timestamp(origin) - tslib.Timestamp(0)

pandas/tests/indexes/datetimes/test_tools.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import dateutil
99
import numpy as np
1010
from dateutil.parser import parse
11-
from datetime import datetime, date, time
11+
from datetime import datetime, date, time, timezone
1212
from distutils.version import LooseVersion
1313

1414
import pandas as pd
@@ -1589,6 +1589,12 @@ def test_invalid_origins(self, origin, exc, units, units_from_epochs):
15891589
pd.to_datetime(units_from_epochs, unit=units,
15901590
origin=origin)
15911591

1592+
def test_invalid_origins_tzinfo(self):
1593+
# GH16842
1594+
with pytest.raises(ValueError):
1595+
pd.to_datetime(1, unit='D',
1596+
origin=datetime(2000, 1, 1, tzinfo=timezone.utc))
1597+
15921598
def test_processing_order(self):
15931599
# make sure we handle out-of-bounds *before*
15941600
# constructing the dates

0 commit comments

Comments
 (0)