Skip to content

Commit 96f92eb

Browse files
ivybaejreback
authored andcommitted
BUG: Cannot use tz-aware origin in to_datetime (#16842)
closes #16842 Author: step4me <[email protected]> Closes #17244 from step4me/step4me-feature and squashes the following commits: 09d051d [step4me] BUG: Cannot use tz-aware origin in to_datetime (#16842)
1 parent 62527c0 commit 96f92eb

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

doc/source/whatsnew/v0.21.0.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ Other API Changes
292292
- The signature of :func:`Series.set_axis` and :func:`DataFrame.set_axis` has been changed from ``set_axis(axis, labels)`` to ``set_axis(labels, axis=0)``, for consistency with the rest of the API. The old signature is deprecated and will show a ``FutureWarning`` (:issue:`14636`)
293293
- :func:`Series.argmin` and :func:`Series.argmax` will now raise a ``TypeError`` when used with ``object`` dtypes, instead of a ``ValueError`` (:issue:`13595`)
294294
- :class:`Period` is now immutable, and will now raise an ``AttributeError`` when a user tries to assign a new value to the ``ordinal`` or ``freq`` attributes (:issue:`17116`).
295+
- :func:`to_datetime` when passed a tz-aware ``origin=`` kwarg will now raise a more informative ``ValueError`` rather than a ``TypeError`` (:issue:`16842`)
295296

296297

297298
.. _whatsnew_0210.deprecations:
@@ -356,6 +357,7 @@ Indexing
356357
- Avoids ``IndexError`` when passing an Index or Series to ``.iloc`` with older numpy (:issue:`17193`)
357358
- Allow unicode empty strings as placeholders in multilevel columns in Python 2 (:issue:`17099`)
358359
- Bug in ``.iloc`` when used with inplace addition or assignment and an int indexer on a ``MultiIndex`` causing the wrong indexes to be read from and written to (:issue:`17148`)
360+
- Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`)
359361

360362
I/O
361363
^^^
@@ -402,6 +404,7 @@ Reshaping
402404
- Fixes dtype of result with integer dtype input, from :func:`pivot_table` when called with ``margins=True`` (:issue:`17013`)
403405
- Bug in :func:`crosstab` where passing two ``Series`` with the same name raised a ``KeyError`` (:issue:`13279`)
404406
- :func:`Series.argmin`, :func:`Series.argmax`, and their counterparts on ``DataFrame`` and groupby objects work correctly with floating point data that contains infinite values (:issue:`13595`).
407+
- Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` (:issue:`17108`)
405408

406409
Numeric
407410
^^^^^^^
@@ -420,5 +423,3 @@ Categorical
420423
Other
421424
^^^^^
422425
- Bug in :func:`eval` where the ``inplace`` parameter was being incorrectly handled (:issue:`16732`)
423-
- Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`)
424-
- Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` (:issue:`17108`)

pandas/core/tools/datetimes.py

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

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

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

pandas/tests/indexes/datetimes/test_tools.py

+6
Original file line numberDiff line numberDiff line change
@@ -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=pytz.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)