From 6cbd748435100ae5392f40ee055d920d4a7fb977 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 1 Jun 2020 16:19:50 -0700 Subject: [PATCH 1/4] DEPR: tz kwarg in Period.tz_convert --- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/_libs/tslibs/period.pyx | 11 +++++++++++ pandas/tests/scalar/period/test_period.py | 21 ++++++++++++++------- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 6e8cbc34be062..c614a5a774518 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -702,6 +702,7 @@ Deprecations raise an ``IndexError`` in the future. You can manually convert to an integer key instead (:issue:`34191`). - The ``squeeze`` keyword in the ``groupby`` function is deprecated and will be removed in a future version (:issue:`32380`) +- The ``tz`` keyword in :meth:`Period.to_timestamp` is deprecated and will be removed in a future version; use `per.to_timestamp(...).tz_localize(tz)`` instead (:issue:`????`) .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index bc190825214c1..620a33bc652d6 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1,3 +1,5 @@ +import warnings + from cpython.object cimport PyObject_RichCompareBool, Py_EQ, Py_NE from numpy cimport int64_t, import_array, ndarray @@ -1724,6 +1726,15 @@ cdef class _Period: ------- Timestamp """ + if tz is not None: + warnings.warn( + "Period.to_timestamp `tz` argument is deprecated and will " + "be removed in a future version. Use " + "`per.to_timestamp(...).tz_localize(tz)` instead.", + FutureWarning, + stacklevel=2, + ) + how = validate_end_alias(how) end = how == 'E' diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 41909b4b1a9bb..93653c0df2b93 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -506,7 +506,8 @@ def test_hash(self): @pytest.mark.parametrize("tzstr", ["Europe/Brussels", "Asia/Tokyo", "US/Pacific"]) def test_to_timestamp_tz_arg(self, tzstr): - p = Period("1/1/2005", freq="M").to_timestamp(tz=tzstr) + with tm.assert_produces_warning(FutureWarning): + p = Period("1/1/2005", freq="M").to_timestamp(tz=tzstr) exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr) exp_zone = pytz.timezone(tzstr).normalize(p) @@ -514,7 +515,8 @@ def test_to_timestamp_tz_arg(self, tzstr): assert p.tz == exp_zone.tzinfo assert p.tz == exp.tz - p = Period("1/1/2005", freq="3H").to_timestamp(tz=tzstr) + with tm.assert_produces_warning(FutureWarning): + p = Period("1/1/2005", freq="3H").to_timestamp(tz=tzstr) exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr) exp_zone = pytz.timezone(tzstr).normalize(p) @@ -522,7 +524,8 @@ def test_to_timestamp_tz_arg(self, tzstr): assert p.tz == exp_zone.tzinfo assert p.tz == exp.tz - p = Period("1/1/2005", freq="A").to_timestamp(freq="A", tz=tzstr) + with tm.assert_produces_warning(FutureWarning): + p = Period("1/1/2005", freq="A").to_timestamp(freq="A", tz=tzstr) exp = Timestamp("31/12/2005", tz="UTC").tz_convert(tzstr) exp_zone = pytz.timezone(tzstr).normalize(p) @@ -530,7 +533,8 @@ def test_to_timestamp_tz_arg(self, tzstr): assert p.tz == exp_zone.tzinfo assert p.tz == exp.tz - p = Period("1/1/2005", freq="A").to_timestamp(freq="3H", tz=tzstr) + with tm.assert_produces_warning(FutureWarning): + p = Period("1/1/2005", freq="A").to_timestamp(freq="3H", tz=tzstr) exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr) exp_zone = pytz.timezone(tzstr).normalize(p) @@ -544,20 +548,23 @@ def test_to_timestamp_tz_arg(self, tzstr): ) def test_to_timestamp_tz_arg_dateutil(self, tzstr): tz = maybe_get_tz(tzstr) - p = Period("1/1/2005", freq="M").to_timestamp(tz=tz) + with tm.assert_produces_warning(FutureWarning): + p = Period("1/1/2005", freq="M").to_timestamp(tz=tz) exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr) assert p == exp assert p.tz == dateutil_gettz(tzstr.split("/", 1)[1]) assert p.tz == exp.tz - p = Period("1/1/2005", freq="M").to_timestamp(freq="3H", tz=tz) + with tm.assert_produces_warning(FutureWarning): + p = Period("1/1/2005", freq="M").to_timestamp(freq="3H", tz=tz) exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr) assert p == exp assert p.tz == dateutil_gettz(tzstr.split("/", 1)[1]) assert p.tz == exp.tz def test_to_timestamp_tz_arg_dateutil_from_string(self): - p = Period("1/1/2005", freq="M").to_timestamp(tz="dateutil/Europe/Brussels") + with tm.assert_produces_warning(FutureWarning): + p = Period("1/1/2005", freq="M").to_timestamp(tz="dateutil/Europe/Brussels") assert p.tz == dateutil_gettz("Europe/Brussels") def test_to_timestamp_mult(self): From 9485e62ccb3f74ba2d7ed6ae14c5c18c837e5a1d Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 1 Jun 2020 16:21:26 -0700 Subject: [PATCH 2/4] update with GH ref --- doc/source/whatsnew/v1.1.0.rst | 2 +- pandas/_libs/tslibs/period.pyx | 1 + pandas/tests/scalar/period/test_period.py | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index c614a5a774518..2230c6cb88843 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -702,7 +702,7 @@ Deprecations raise an ``IndexError`` in the future. You can manually convert to an integer key instead (:issue:`34191`). - The ``squeeze`` keyword in the ``groupby`` function is deprecated and will be removed in a future version (:issue:`32380`) -- The ``tz`` keyword in :meth:`Period.to_timestamp` is deprecated and will be removed in a future version; use `per.to_timestamp(...).tz_localize(tz)`` instead (:issue:`????`) +- The ``tz`` keyword in :meth:`Period.to_timestamp` is deprecated and will be removed in a future version; use `per.to_timestamp(...).tz_localize(tz)`` instead (:issue:`34522`) .. --------------------------------------------------------------------------- diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 620a33bc652d6..36b5c53ce9b35 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1727,6 +1727,7 @@ cdef class _Period: Timestamp """ if tz is not None: + # GH#34522 warnings.warn( "Period.to_timestamp `tz` argument is deprecated and will " "be removed in a future version. Use " diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index 93653c0df2b93..42bd20fd9640b 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -506,6 +506,7 @@ def test_hash(self): @pytest.mark.parametrize("tzstr", ["Europe/Brussels", "Asia/Tokyo", "US/Pacific"]) def test_to_timestamp_tz_arg(self, tzstr): + # GH#34522 tz kwarg deprecated with tm.assert_produces_warning(FutureWarning): p = Period("1/1/2005", freq="M").to_timestamp(tz=tzstr) exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr) From 441f8d823916f012f7f4f36a4c3f0ee31bad019d Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 2 Jun 2020 11:16:14 -0700 Subject: [PATCH 3/4] fix stacklevle --- pandas/_libs/tslibs/period.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 36b5c53ce9b35..e271518525008 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1733,7 +1733,7 @@ cdef class _Period: "be removed in a future version. Use " "`per.to_timestamp(...).tz_localize(tz)` instead.", FutureWarning, - stacklevel=2, + stacklevel=1, ) how = validate_end_alias(how) From e67b3fca9e4f79fe64dd771ee6a251aa07842f82 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 2 Jun 2020 13:40:03 -0700 Subject: [PATCH 4/4] update usage in plotting --- pandas/plotting/_matplotlib/timeseries.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/timeseries.py b/pandas/plotting/_matplotlib/timeseries.py index 631760c547985..fc7fd037bebdd 100644 --- a/pandas/plotting/_matplotlib/timeseries.py +++ b/pandas/plotting/_matplotlib/timeseries.py @@ -221,7 +221,7 @@ def _use_dynamic_x(ax, data): x = data.index if base <= FreqGroup.FR_DAY: return x[:1].is_normalized - return Period(x[0], freq).to_timestamp(tz=x.tz) == x[0] + return Period(x[0], freq).to_timestamp().tz_localize(x.tz) == x[0] return True