diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index e944189e2a338..aab9e6125732a 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -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) diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index 2c5620769d1b4..b83c51b6a3ab6 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -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 @@ -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): diff --git a/pandas/tseries/tools.py b/pandas/tseries/tools.py index 2c7f1ba8c6787..8f127e28e28a9 100644 --- a/pandas/tseries/tools.py +++ b/pandas/tseries/tools.py @@ -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':