Skip to content

BUG: Return UTC DateTimeIndex when specified in to_datetime #12391

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,4 @@ Bug Fixes

- Bug in ``DataFrame.apply`` in which reduction was not being prevented for cases in which ``dtype`` was not a numpy dtype (:issue:`12244`)
- Bug when initializing categorical series with a scalar value. (:issue:`12336`)
- Bug when specifying a UTC ``DatetimeIndex`` by setting ``utc=True`` in ``.to_datetime`` (:issue:11934)
13 changes: 12 additions & 1 deletion pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,16 @@ def test_to_datetime_tz_pytz(self):
dtype='datetime64[ns, UTC]', freq=None)
tm.assert_index_equal(result, expected)

def test_to_datetime_utc_is_true(self):
# See gh-11934
start = pd.Timestamp('2014-01-01', tz='utc')
end = pd.Timestamp('2014-01-03', tz='utc')
date_range = pd.bdate_range(start, end)

result = pd.to_datetime(date_range, utc=True)
expected = pd.DatetimeIndex(data=date_range)
tm.assert_index_equal(result, expected)

def test_to_datetime_tz_psycopg2(self):

# xref 8260
Expand Down Expand Up @@ -1175,7 +1185,8 @@ def test_to_datetime_tz_psycopg2(self):
tm.assert_index_equal(result, i)

result = pd.to_datetime(i, errors='coerce', utc=True)
expected = pd.DatetimeIndex(['2000-01-01 13:00:00'])
expected = pd.DatetimeIndex(['2000-01-01 13:00:00'],
dtype='datetime64[ns, UTC]')
tm.assert_index_equal(result, expected)

def test_index_to_datetime(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def _convert_listlike(arg, box, format, name=None):
if not isinstance(arg, DatetimeIndex):
return DatetimeIndex(arg, tz='utc' if utc else None)
if utc:
arg = arg.tz_convert(None)
arg = arg.tz_convert(None).tz_localize('UTC')
return arg

elif format is None and com.is_integer_dtype(arg) and unit == 'ns':
Expand Down