Skip to content

BUG: fix to_datetime(dti, utc=True) #27733

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 5 commits into from
Aug 5, 2019
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
3 changes: 3 additions & 0 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ def _convert_listlike_datetimes(
return DatetimeIndex(arg, tz=tz, name=name)
except ValueError:
pass
elif box and tz:
# DatetimeArray, DatetimeIndex
return arg.tz_localize(tz)

return arg

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,16 @@ def test_dayfirst(self, cache):
tm.assert_index_equal(expected, idx5)
tm.assert_index_equal(expected, idx6)

def test_to_datetime_dta_tz(self):
dti = date_range("2015-04-05", periods=3).rename("foo")
expected = dti.tz_localize("UTC")
result = to_datetime(dti, utc=True)
tm.assert_index_equal(result, expected)

dta = dti._data
result = to_datetime(dta, utc=True)
tm.assert_equal(result, expected._data)


class TestGuessDatetimeFormat:
@td.skip_if_not_us_locale
Expand Down