From 167b6a314fede69512e8f3f88138e4f46e6e3a82 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 29 Nov 2019 12:22:49 -0800 Subject: [PATCH 1/3] DEPR: tz_convert in the Timestamp constructor raises --- doc/source/whatsnew/v1.0.0.rst | 1 + pandas/_libs/tslibs/timestamps.pyx | 5 ++--- pandas/tests/scalar/timestamp/test_timestamp.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 14f36a808c468..b60c8b99658ad 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -457,6 +457,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. - Removed previously deprecated keyword "n" from :meth:`DatetimeIndex.shift`, :meth:`TimedeltaIndex.shift`, :meth:`PeriodIndex.shift`, use "periods" instead (:issue:`22458`) - Changed the default value for the `raw` argument in :func:`Series.rolling().apply() `, :func:`DataFrame.rolling().apply() `, - :func:`Series.expanding().apply() `, and :func:`DataFrame.expanding().apply() ` to ``False`` (:issue:`20584`) +- Timezone converting a tz-aware ``datetime.datetime`` or :class:`Timestamp` with :class:`Timestamp` and the ``tz`` argument now raises a ``ValueError`` (:issue:`23621`) - Removed previously deprecated :attr:`Timestamp.weekday_name`, :attr:`DatetimeIndex.weekday_name`, and :attr:`Series.dt.weekday_name` (:issue:`18164`) - Removed previously deprecated ``errors`` argument in :meth:`Timestamp.tz_localize`, :meth:`DatetimeIndex.tz_localize`, and :meth:`Series.tz_localize` (:issue:`22644`) - diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index e7dc911ff0bae..aa7c6fbd0e4d6 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -401,9 +401,8 @@ class Timestamp(_Timestamp): freq = None if getattr(ts_input, 'tzinfo', None) is not None and tz is not None: - warnings.warn("Passing a datetime or Timestamp with tzinfo and the" - " tz parameter will raise in the future. Use" - " tz_convert instead.", FutureWarning) + raise ValueError("Cannot pass a datetime or Timestamp with tzinfo with the" + " tz parameter. Use tz_convert instead.") ts = convert_to_tsobject(ts_input, tz, unit, 0, 0, nanosecond or 0) diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index a33afc8b3ccca..f0c88157dedfa 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -671,10 +671,10 @@ def test_constructor_invalid_frequency(self): Timestamp("2012-01-01", freq=[]) @pytest.mark.parametrize("box", [datetime, Timestamp]) - def test_depreciate_tz_and_tzinfo_in_datetime_input(self, box): + def test_raise_tz_and_tzinfo_in_datetime_input(self, box): # GH 23579 kwargs = {"year": 2018, "month": 1, "day": 1, "tzinfo": utc} - with tm.assert_produces_warning(FutureWarning): + with pytest.raises(ValueError, match="Cannot pass a datetime or Timestamp"): Timestamp(box(**kwargs), tz="US/Pacific") def test_dont_convert_dateutil_utc_to_pytz_utc(self): From 76793eb482f36a8d9e49a112e31a5b699a1af026 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 29 Nov 2019 21:46:35 -0800 Subject: [PATCH 2/3] Address comments --- doc/source/whatsnew/v1.0.0.rst | 2 +- pandas/tests/scalar/timestamp/test_timestamp.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index b60c8b99658ad..f100bead82d7a 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -457,7 +457,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. - Removed previously deprecated keyword "n" from :meth:`DatetimeIndex.shift`, :meth:`TimedeltaIndex.shift`, :meth:`PeriodIndex.shift`, use "periods" instead (:issue:`22458`) - Changed the default value for the `raw` argument in :func:`Series.rolling().apply() `, :func:`DataFrame.rolling().apply() `, - :func:`Series.expanding().apply() `, and :func:`DataFrame.expanding().apply() ` to ``False`` (:issue:`20584`) -- Timezone converting a tz-aware ``datetime.datetime`` or :class:`Timestamp` with :class:`Timestamp` and the ``tz`` argument now raises a ``ValueError`` (:issue:`23621`) +- Passing a tz-aware ``datetime.datetime`` or :class:`Timestamp` into the :class:`Timestamp` constructor with the ``tz`` argument now raises a ``ValueError`` (:issue:`23621`) - Removed previously deprecated :attr:`Timestamp.weekday_name`, :attr:`DatetimeIndex.weekday_name`, and :attr:`Series.dt.weekday_name` (:issue:`18164`) - Removed previously deprecated ``errors`` argument in :meth:`Timestamp.tz_localize`, :meth:`DatetimeIndex.tz_localize`, and :meth:`Series.tz_localize` (:issue:`22644`) - diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index f0c88157dedfa..3dda2027b394d 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -676,6 +676,7 @@ def test_raise_tz_and_tzinfo_in_datetime_input(self, box): kwargs = {"year": 2018, "month": 1, "day": 1, "tzinfo": utc} with pytest.raises(ValueError, match="Cannot pass a datetime or Timestamp"): Timestamp(box(**kwargs), tz="US/Pacific") + Timestamp(box(**kwargs), tzinfo="US/Pacific") def test_dont_convert_dateutil_utc_to_pytz_utc(self): result = Timestamp(datetime(2018, 1, 1), tz=tzutc()) From c349cf0ebfe378c2270551edfb987a880599fc5d Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sat, 30 Nov 2019 13:51:11 -0800 Subject: [PATCH 3/3] Add another context manager + test --- pandas/tests/scalar/timestamp/test_timestamp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index f996d12175896..512a83ed304d1 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -680,7 +680,8 @@ def test_raise_tz_and_tzinfo_in_datetime_input(self, box): kwargs = {"year": 2018, "month": 1, "day": 1, "tzinfo": utc} with pytest.raises(ValueError, match="Cannot pass a datetime or Timestamp"): Timestamp(box(**kwargs), tz="US/Pacific") - Timestamp(box(**kwargs), tzinfo="US/Pacific") + with pytest.raises(ValueError, match="Cannot pass a datetime or Timestamp"): + Timestamp(box(**kwargs), tzinfo=pytz.timezone("US/Pacific")) def test_dont_convert_dateutil_utc_to_pytz_utc(self): result = Timestamp(datetime(2018, 1, 1), tz=tzutc())