From e5030b39bf06e7bca543fc16dc83256593e6d198 Mon Sep 17 00:00:00 2001 From: step4me Date: Thu, 24 Aug 2017 08:53:50 -0400 Subject: [PATCH] BUG: Cannot use tz-aware origin in to_datetime (#16842) closes #16842 Author: step4me Closes #17244 from step4me/step4me-feature and squashes the following commits: 09d051d48 [step4me] BUG: Cannot use tz-aware origin in to_datetime (#16842) --- doc/source/whatsnew/v0.21.0.txt | 5 +++-- pandas/core/tools/datetimes.py | 7 ++++++- pandas/tests/indexes/datetimes/test_tools.py | 6 ++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 2850c98c64d78..7748f437347a6 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -293,6 +293,7 @@ Other API Changes - 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`) - :func:`Series.argmin` and :func:`Series.argmax` will now raise a ``TypeError`` when used with ``object`` dtypes, instead of a ``ValueError`` (:issue:`13595`) - :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`). +- :func:`to_datetime` when passed a tz-aware ``origin=`` kwarg will now raise a more informative ``ValueError`` rather than a ``TypeError`` (:issue:`16842`) .. _whatsnew_0210.deprecations: @@ -357,6 +358,7 @@ Indexing - Avoids ``IndexError`` when passing an Index or Series to ``.iloc`` with older numpy (:issue:`17193`) - Allow unicode empty strings as placeholders in multilevel columns in Python 2 (:issue:`17099`) - 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`) +- Bug in ``.isin()`` in which checking membership in empty ``Series`` objects raised an error (:issue:`16991`) I/O ^^^ @@ -403,6 +405,7 @@ Reshaping - Fixes dtype of result with integer dtype input, from :func:`pivot_table` when called with ``margins=True`` (:issue:`17013`) - Bug in :func:`crosstab` where passing two ``Series`` with the same name raised a ``KeyError`` (:issue:`13279`) - :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`). +- Bug in :func:`unique` where checking a tuple of strings raised a ``TypeError`` (:issue:`17108`) Numeric ^^^^^^^ @@ -421,5 +424,3 @@ Categorical 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`) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 53f58660cabdb..c0f234a36803d 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -489,7 +489,7 @@ 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 {origin} is Out of Bounds".format(origin=origin)) @@ -497,6 +497,11 @@ def _convert_listlike(arg, box, format, name=None, tz=tz): raise ValueError("origin {origin} cannot be converted " "to a Timestamp".format(origin=origin)) + if offset.tz is not None: + raise ValueError( + "origin offset {} must be tz-naive".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) diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index 9764b65d330af..50669ee357bbd 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -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