Skip to content

Commit a199cb3

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

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
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 kwarg (:issue:`16842`)

pandas/core/tools/datetimes.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,19 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
488488

489489
# we are going to offset back to unix / epoch time
490490
try:
491-
offset = tslib.Timestamp(origin) - tslib.Timestamp(0)
491+
offset = tslib.Timestamp(origin)
492492
except tslib.OutOfBoundsDatetime:
493493
raise tslib.OutOfBoundsDatetime(
494494
"origin {} is Out of Bounds".format(origin))
495495
except ValueError:
496496
raise ValueError("origin {} cannot be converted "
497497
"to a Timestamp".format(origin))
498498

499+
if offset.tz is not None:
500+
raise ValueError(
501+
"offset {} must have no timezone".format(offset))
502+
offset -= tslib.Timestamp(0)
503+
499504
# convert the offset to the unit of the arg
500505
# this should be lossless in terms of precision
501506
offset = offset // tslib.Timedelta(1, unit=unit)

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)