8000 BUG: to_datetime(Timestamp, utc=True) localizes to UTC by mroeschke · Pull Request #24441 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Next Next commit
allow tz-aware timestamp to be converted
  • Loading branch information
Matt Roeschke committed Dec 26, 2018
commit 081fded990df919a875e19e7f2496c176c953cfd
5 changes: 4 additions & 1 deletion pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,10 @@ 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)
if arg.tz is not None:
result = result.tz_convert(tz)
else:
result = result.tz_localize(tz)
elif isinstance(arg, ABCSeries):
cache_array = _maybe_cache(arg, format, cache, convert_listlike)
if not cache_array.empty:
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,10 @@ def test_timestamp_utc_true(self):
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)
ts = ts.tz_localize('US/Pacific')
result = to_datetime(ts, utc=True)
expected = ts.tz_convert('UTC')
assert result == expected


class TestToDatetimeUnit(object):
Expand Down
0