Skip to content

Commit caa0a62

Browse files
author
Matt Roeschke
committed
DEPR: tz_convert in the Timestamp constructor
1 parent b930303 commit caa0a62

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

doc/source/whatsnew/v0.24.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ Deprecations
967967
- The class ``FrozenNDArray`` has been deprecated. When unpickling, ``FrozenNDArray`` will be unpickled to ``np.ndarray`` once this class is removed (:issue:`9031`)
968968
- Deprecated the `nthreads` keyword of :func:`pandas.read_feather` in favor of
969969
`use_threads` to reflect the changes in pyarrow 0.11.0. (:issue:`23053`)
970+
- Converting a tz-aware :class:`Timestamp` with :class:`Timestamp` and the ``tz`` argument is now deprecated. Instead use :meth:`Timestamp.tz_convert` (:issue:`23579`)
970971

971972
.. _whatsnew_0240.deprecations.datetimelike_int_ops:
972973

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,6 @@ class Timestamp(_Timestamp):
709709

710710
elif ts_input is _no_input:
711711
# User passed keyword arguments.
712-
if tz is None:
713-
# Handle the case where the user passes `tz` and not `tzinfo`
714-
tz = tzinfo
715712
return Timestamp(datetime(year, month, day, hour or 0,
716713
minute or 0, second or 0,
717714
microsecond or 0, tzinfo),
@@ -728,6 +725,11 @@ class Timestamp(_Timestamp):
728725
# User passed tzinfo instead of tz; avoid silently ignoring
729726
tz, tzinfo = tzinfo, None
730727

728+
if getattr(ts_input, 'tzinfo', None) is not None and tz is not None:
729+
warnings.warn("Passing a datetime or Timestamp with tzinfo and the"
730+
" tz parameter will raise in the future. Use"
731+
" tz_convert instead.", FutureWarning)
732+
731733
ts = convert_to_tsobject(ts_input, tz, unit, 0, 0, nanosecond or 0)
732734

733735
if ts.value == NPY_NAT:

pandas/tests/scalar/timestamp/test_timestamp.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ def test_constructor(self):
244244
assert conversion.pydt_to_i8(result) == expected_tz
245245

246246
# should convert to UTC
247-
result = Timestamp(result, tz='UTC')
247+
if tz is not None:
248+
result = Timestamp(result).tz_convert('UTC')
249+
else:
250+
result = Timestamp(result, tz='UTC')
248251
expected_utc = expected - offset * 3600 * 1000000000
249252
assert result.value == expected_utc
250253
assert conversion.pydt_to_i8(result) == expected_utc
@@ -295,7 +298,7 @@ def test_constructor_with_stringoffset(self):
295298
assert conversion.pydt_to_i8(result) == expected_tz
296299

297300
# should convert to UTC
298-
result = Timestamp(result, tz='UTC')
301+
result = Timestamp(result).tz_convert('UTC')
299302
expected_utc = expected
300303
assert result.value == expected_utc
301304
assert conversion.pydt_to_i8(result) == expected_utc
@@ -558,7 +561,7 @@ def test_construct_timestamp_near_dst(self, offset):
558561
# GH 20854
559562
expected = Timestamp('2016-10-30 03:00:00{}'.format(offset),
560563
tz='Europe/Helsinki')
561-
result = Timestamp(expected, tz='Europe/Helsinki')
564+
result = Timestamp(expected).tz_convert('Europe/Helsinki')
562565
assert result == expected
563566

564567
@pytest.mark.parametrize('arg', [
@@ -580,6 +583,13 @@ def test_constructor_invalid_frequency(self):
580583
with tm.assert_raises_regex(ValueError, "Invalid frequency:"):
581584
Timestamp('2012-01-01', freq=[])
582585

586+
@pytest.mark.parametrize('box', [datetime, Timestamp])
587+
def test_depreciate_tz_and_tzinfo_in_datetime_input(self, box):
588+
# GH 23579
589+
kwargs = {'year': 2018, 'month': 1, 'day': 1, 'tzinfo': utc}
590+
with tm.assert_produces_warning(FutureWarning):
591+
Timestamp(box(**kwargs), tz='US/Pacific')
592+
583593

584594
class TestTimestamp(object):
585595

0 commit comments

Comments
 (0)