Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: tz kwarg in Period.to_timestamp #34522

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:`34522`)

.. ---------------------------------------------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from cpython.object cimport PyObject_RichCompareBool, Py_EQ, Py_NE

from numpy cimport int64_t, import_array, ndarray
Expand Down Expand Up @@ -1724,6 +1726,16 @@ 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 "
"`per.to_timestamp(...).tz_localize(tz)` instead.",
FutureWarning,
stacklevel=1,
)

how = validate_end_alias(how)

end = how == 'E'
Expand Down
2 changes: 1 addition & 1 deletion pandas/plotting/_matplotlib/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
22 changes: 15 additions & 7 deletions pandas/tests/scalar/period/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,31 +506,36 @@ 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)
# 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

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)

assert p == exp
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)

assert p == exp
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)

Expand All @@ -544,20 +549,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):
Expand Down