From e542968328b65a8d47100fb7bdc65e1601d7d218 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 24 Oct 2022 13:29:04 -0700 Subject: [PATCH] DEPR: tz arg in Period.to_timestamp (#49280) --- doc/source/whatsnew/v2.0.0.rst | 1 + pandas/_libs/tslibs/period.pyi | 2 - pandas/_libs/tslibs/period.pyx | 18 +----- pandas/tests/scalar/period/test_period.py | 69 ----------------------- 4 files changed, 3 insertions(+), 87 deletions(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 19018f300b59a..b29284c887c3d 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -181,6 +181,7 @@ Removal of prior version deprecations/changes - Removed :meth:`MultiIndex.is_lexsorted` and :meth:`MultiIndex.lexsort_depth` (:issue:`38701`) - Removed argument ``how`` from :meth:`PeriodIndex.astype`, use :meth:`PeriodIndex.to_timestamp` instead (:issue:`37982`) - Removed argument ``try_cast`` from :meth:`DataFrame.mask`, :meth:`DataFrame.where`, :meth:`Series.mask` and :meth:`Series.where` (:issue:`38836`) +- Removed argument ``tz`` from :meth:`Period.to_timestamp`, use ``obj.to_timestamp(...).tz_localize(tz)`` instead (:issue:`34522`) - Removed argument ``is_copy`` from :meth:`DataFrame.take` and :meth:`Series.take` (:issue:`30615`) - Removed argument ``kind`` from :meth:`Index.get_slice_bound`, :meth:`Index.slice_indexer` and :meth:`Index.slice_locs` (:issue:`41378`) - Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`) diff --git a/pandas/_libs/tslibs/period.pyi b/pandas/_libs/tslibs/period.pyi index 5ad919649262c..946ae1215f1e3 100644 --- a/pandas/_libs/tslibs/period.pyi +++ b/pandas/_libs/tslibs/period.pyi @@ -8,7 +8,6 @@ from pandas._libs.tslibs.offsets import BaseOffset from pandas._libs.tslibs.timestamps import Timestamp from pandas._typing import ( Frequency, - Timezone, npt, ) @@ -88,7 +87,6 @@ class Period(PeriodMixin): self, freq: str | BaseOffset | None = ..., how: str = ..., - tz: Timezone | None = ..., ) -> Timestamp: ... def asfreq(self, freq: str | BaseOffset, how: str = ...) -> Period: ... @property diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index a516565ff3f8d..be6f87791284e 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1,7 +1,3 @@ -import warnings - -from pandas.util._exceptions import find_stack_level - cimport numpy as cnp from cpython.object cimport ( Py_EQ, @@ -1802,7 +1798,7 @@ cdef class _Period(PeriodMixin): return Period(ordinal=ordinal, freq=freq) - def to_timestamp(self, freq=None, how='start', tz=None) -> Timestamp: + def to_timestamp(self, freq=None, how="start") -> Timestamp: """ Return the Timestamp representation of the Period. @@ -1822,16 +1818,6 @@ cdef class _Period(PeriodMixin): ------- 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 " - "`per.to_timestamp(...).tz_localize(tz)` instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - how = validate_end_alias(how) end = how == 'E' @@ -1853,7 +1839,7 @@ cdef class _Period(PeriodMixin): val = self.asfreq(freq, how) dt64 = period_ordinal_to_dt64(val.ordinal, base) - return Timestamp(dt64, tz=tz) + return Timestamp(dt64) @property def year(self) -> int: diff --git a/pandas/tests/scalar/period/test_period.py b/pandas/tests/scalar/period/test_period.py index b8baaaeaeecf8..b5491c606ec7b 100644 --- a/pandas/tests/scalar/period/test_period.py +++ b/pandas/tests/scalar/period/test_period.py @@ -6,7 +6,6 @@ import numpy as np import pytest -import pytz from pandas._libs.tslibs import ( iNaT, @@ -22,10 +21,6 @@ INVALID_FREQ_ERR_MSG, IncompatibleFrequency, ) -from pandas._libs.tslibs.timezones import ( - dateutil_gettz, - maybe_get_tz, -) import pandas as pd from pandas import ( @@ -583,70 +578,6 @@ def test_hash(self): # -------------------------------------------------------------- # to_timestamp - @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) - exp_zone = pytz.timezone(tzstr).normalize(p) - - assert p == exp - assert p.tz == exp_zone.tzinfo - assert p.tz == exp.tz - - 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) - - assert p == exp - assert p.tz == exp_zone.tzinfo - assert p.tz == exp.tz - - with tm.assert_produces_warning(FutureWarning): - p = Period("1/1/2005", freq="A").to_timestamp(freq="A", tz=tzstr) - exp = Timestamp(day=31, month=12, year=2005, tz="UTC").tz_convert(tzstr) - exp_zone = pytz.timezone(tzstr).normalize(p) - - assert p == exp - assert p.tz == exp_zone.tzinfo - assert p.tz == exp.tz - - 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) - - assert p == exp - assert p.tz == exp_zone.tzinfo - assert p.tz == exp.tz - - @pytest.mark.parametrize( - "tzstr", - ["dateutil/Europe/Brussels", "dateutil/Asia/Tokyo", "dateutil/US/Pacific"], - ) - def test_to_timestamp_tz_arg_dateutil(self, tzstr): - tz = maybe_get_tz(tzstr) - 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 - - 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): - 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): p = Period("2011-01", freq="M") assert p.to_timestamp(how="S") == Timestamp("2011-01-01")