Skip to content

DEPR: tz_convert in the Timestamp constructor #23621

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

Merged
merged 16 commits into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ Deprecations
- The class ``FrozenNDArray`` has been deprecated. When unpickling, ``FrozenNDArray`` will be unpickled to ``np.ndarray`` once this class is removed (:issue:`9031`)
- Deprecated the `nthreads` keyword of :func:`pandas.read_feather` in favor of
`use_threads` to reflect the changes in pyarrow 0.11.0. (:issue:`23053`)
- Timezone converting a tz-aware ``datetime.datetime`` or :class:`Timestamp` with :class:`Timestamp` and the ``tz`` argument is now deprecated. Instead use :meth:`Timestamp.tz_convert` (:issue:`23579`)

.. _whatsnew_0240.deprecations.datetimelike_int_ops:

Expand Down
8 changes: 5 additions & 3 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,6 @@ class Timestamp(_Timestamp):

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

if getattr(ts_input, 'tzinfo', None) is not None and tz is not None:
warnings.warn("Passing a datetime or Timestamp with tzinfo and the"
" tz parameter will raise in the future. Use"
" tz_convert instead.", FutureWarning)

ts = convert_to_tsobject(ts_input, tz, unit, 0, 0, nanosecond or 0)

if ts.value == NPY_NAT:
Expand Down
16 changes: 13 additions & 3 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ def test_constructor(self):
assert conversion.pydt_to_i8(result) == expected_tz

# should convert to UTC
result = Timestamp(result, tz='UTC')
if tz is not None:
result = Timestamp(result).tz_convert('UTC')
else:
result = Timestamp(result, tz='UTC')
expected_utc = expected - offset * 3600 * 1000000000
assert result.value == expected_utc
assert conversion.pydt_to_i8(result) == expected_utc
Expand Down Expand Up @@ -295,7 +298,7 @@ def test_constructor_with_stringoffset(self):
assert conversion.pydt_to_i8(result) == expected_tz

# should convert to UTC
result = Timestamp(result, tz='UTC')
result = Timestamp(result).tz_convert('UTC')
expected_utc = expected
assert result.value == expected_utc
assert conversion.pydt_to_i8(result) == expected_utc
Expand Down Expand Up @@ -558,7 +561,7 @@ def test_construct_timestamp_near_dst(self, offset):
# GH 20854
expected = Timestamp('2016-10-30 03:00:00{}'.format(offset),
tz='Europe/Helsinki')
result = Timestamp(expected, tz='Europe/Helsinki')
result = Timestamp(expected).tz_convert('Europe/Helsinki')
assert result == expected

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

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


class TestTimestamp(object):

Expand Down