Skip to content

BUG: to_datetime(Timestamp, utc=True) localizes to UTC #24441

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 4 commits into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ Timezones
- Bug in :meth:`DatetimeIndex.tz_localize` and :meth:`Timestamp.tz_localize` with ``dateutil.tz.tzlocal`` near a DST transition that would return an incorrectly localized datetime (:issue:`23807`)
- Bug in :class:`Timestamp` constructor where a ``dateutil.tz.tzutc`` timezone passed with a ``datetime.datetime`` argument would be converted to a ``pytz.UTC`` timezone (:issue:`23807`)
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when specifying a ``unit`` and ``errors='ignore'`` (:issue:`23758`)
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when passing a :class:`Timestamp` (:issue:`24415`)

Offsets
^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,

if isinstance(arg, Timestamp):
result = arg
if tz is not None:
result = arg.tz_localize(tz)
elif isinstance(arg, ABCSeries):
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
if not cache_array.empty:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,18 @@ def test_non_iso_strings_with_tz_offset(self):
tzinfo=pytz.FixedOffset(240))] * 2)
tm.assert_index_equal(result, expected)

def test_timestamp_utc_true(self):
# GH 24415
ts = Timestamp(2018, 1, 1)
result = to_datetime(ts, utc=True)
expected = ts.tz_localize('UTC')
assert result == expected

msg = ('Cannot localize tz-aware Timestamp, use tz_convert for '
'conversions')
with pytest.raises(TypeError, match=msg):
to_datetime(ts.tz_localize('US/Pacific'), utc=True)


class TestToDatetimeUnit(object):
@pytest.mark.parametrize('cache', [True, False])
Expand Down