Skip to content

Commit e79487d

Browse files
authored
DEPR: tz kwarg in Period.to_timestamp (#34522)
1 parent fb7bf94 commit e79487d

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ Deprecations
741741
raise an ``IndexError`` in the future. You can manually convert to an integer key
742742
instead (:issue:`34191`).
743743
- The ``squeeze`` keyword in the ``groupby`` function is deprecated and will be removed in a future version (:issue:`32380`)
744+
- 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`)
744745

745746
.. ---------------------------------------------------------------------------
746747

pandas/_libs/tslibs/period.pyx

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
from cpython.object cimport PyObject_RichCompareBool, Py_EQ, Py_NE
24

35
from numpy cimport int64_t, import_array, ndarray
@@ -1727,6 +1729,16 @@ cdef class _Period:
17271729
-------
17281730
Timestamp
17291731
"""
1732+
if tz is not None:
1733+
# GH#34522
1734+
warnings.warn(
1735+
"Period.to_timestamp `tz` argument is deprecated and will "
1736+
"be removed in a future version. Use "
1737+
"`per.to_timestamp(...).tz_localize(tz)` instead.",
1738+
FutureWarning,
1739+
stacklevel=1,
1740+
)
1741+
17301742
how = validate_end_alias(how)
17311743

17321744
end = how == 'E'

pandas/plotting/_matplotlib/timeseries.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def _use_dynamic_x(ax, data):
221221
x = data.index
222222
if base <= FreqGroup.FR_DAY:
223223
return x[:1].is_normalized
224-
return Period(x[0], freq).to_timestamp(tz=x.tz) == x[0]
224+
return Period(x[0], freq).to_timestamp().tz_localize(x.tz) == x[0]
225225
return True
226226

227227

pandas/tests/scalar/period/test_period.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -506,31 +506,36 @@ def test_hash(self):
506506

507507
@pytest.mark.parametrize("tzstr", ["Europe/Brussels", "Asia/Tokyo", "US/Pacific"])
508508
def test_to_timestamp_tz_arg(self, tzstr):
509-
p = Period("1/1/2005", freq="M").to_timestamp(tz=tzstr)
509+
# GH#34522 tz kwarg deprecated
510+
with tm.assert_produces_warning(FutureWarning):
511+
p = Period("1/1/2005", freq="M").to_timestamp(tz=tzstr)
510512
exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr)
511513
exp_zone = pytz.timezone(tzstr).normalize(p)
512514

513515
assert p == exp
514516
assert p.tz == exp_zone.tzinfo
515517
assert p.tz == exp.tz
516518

517-
p = Period("1/1/2005", freq="3H").to_timestamp(tz=tzstr)
519+
with tm.assert_produces_warning(FutureWarning):
520+
p = Period("1/1/2005", freq="3H").to_timestamp(tz=tzstr)
518521
exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr)
519522
exp_zone = pytz.timezone(tzstr).normalize(p)
520523

521524
assert p == exp
522525
assert p.tz == exp_zone.tzinfo
523526
assert p.tz == exp.tz
524527

525-
p = Period("1/1/2005", freq="A").to_timestamp(freq="A", tz=tzstr)
528+
with tm.assert_produces_warning(FutureWarning):
529+
p = Period("1/1/2005", freq="A").to_timestamp(freq="A", tz=tzstr)
526530
exp = Timestamp("31/12/2005", tz="UTC").tz_convert(tzstr)
527531
exp_zone = pytz.timezone(tzstr).normalize(p)
528532

529533
assert p == exp
530534
assert p.tz == exp_zone.tzinfo
531535
assert p.tz == exp.tz
532536

533-
p = Period("1/1/2005", freq="A").to_timestamp(freq="3H", tz=tzstr)
537+
with tm.assert_produces_warning(FutureWarning):
538+
p = Period("1/1/2005", freq="A").to_timestamp(freq="3H", tz=tzstr)
534539
exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr)
535540
exp_zone = pytz.timezone(tzstr).normalize(p)
536541

@@ -544,20 +549,23 @@ def test_to_timestamp_tz_arg(self, tzstr):
544549
)
545550
def test_to_timestamp_tz_arg_dateutil(self, tzstr):
546551
tz = maybe_get_tz(tzstr)
547-
p = Period("1/1/2005", freq="M").to_timestamp(tz=tz)
552+
with tm.assert_produces_warning(FutureWarning):
553+
p = Period("1/1/2005", freq="M").to_timestamp(tz=tz)
548554
exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr)
549555
assert p == exp
550556
assert p.tz == dateutil_gettz(tzstr.split("/", 1)[1])
551557
assert p.tz == exp.tz
552558

553-
p = Period("1/1/2005", freq="M").to_timestamp(freq="3H", tz=tz)
559+
with tm.assert_produces_warning(FutureWarning):
560+
p = Period("1/1/2005", freq="M").to_timestamp(freq="3H", tz=tz)
554561
exp = Timestamp("1/1/2005", tz="UTC").tz_convert(tzstr)
555562
assert p == exp
556563
assert p.tz == dateutil_gettz(tzstr.split("/", 1)[1])
557564
assert p.tz == exp.tz
558565

559566
def test_to_timestamp_tz_arg_dateutil_from_string(self):
560-
p = Period("1/1/2005", freq="M").to_timestamp(tz="dateutil/Europe/Brussels")
567+
with tm.assert_produces_warning(FutureWarning):
568+
p = Period("1/1/2005", freq="M").to_timestamp(tz="dateutil/Europe/Brussels")
561569
assert p.tz == dateutil_gettz("Europe/Brussels")
562570

563571
def test_to_timestamp_mult(self):

0 commit comments

Comments
 (0)