Skip to content

Commit e39f63a

Browse files
gfyoungjreback
authored andcommitted
BUG: Return UTC DateTimeIndex when specified in to_datetime
Title is self-explanatory. Closes pandas-dev#11934. Author: gfyoung <[email protected]> Closes pandas-dev#12391 from gfyoung/to_datetime_utc and squashes the following commits: fa794a5 [gfyoung] BUG: Return UTC DateTimeIndex when specified in to_datetime
1 parent 49f99a6 commit e39f63a

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1108,3 +1108,4 @@ Bug Fixes
11081108

11091109
- Bug in ``DataFrame.apply`` in which reduction was not being prevented for cases in which ``dtype`` was not a numpy dtype (:issue:`12244`)
11101110
- Bug when initializing categorical series with a scalar value. (:issue:`12336`)
1111+
- Bug when specifying a UTC ``DatetimeIndex`` by setting ``utc=True`` in ``.to_datetime`` (:issue:11934)

pandas/tseries/tests/test_timeseries.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,16 @@ def test_to_datetime_tz_pytz(self):
11431143
dtype='datetime64[ns, UTC]', freq=None)
11441144
tm.assert_index_equal(result, expected)
11451145

1146+
def test_to_datetime_utc_is_true(self):
1147+
# See gh-11934
1148+
start = pd.Timestamp('2014-01-01', tz='utc')
1149+
end = pd.Timestamp('2014-01-03', tz='utc')
1150+
date_range = pd.bdate_range(start, end)
1151+
1152+
result = pd.to_datetime(date_range, utc=True)
1153+
expected = pd.DatetimeIndex(data=date_range)
1154+
tm.assert_index_equal(result, expected)
1155+
11461156
def test_to_datetime_tz_psycopg2(self):
11471157

11481158
# xref 8260
@@ -1175,7 +1185,8 @@ def test_to_datetime_tz_psycopg2(self):
11751185
tm.assert_index_equal(result, i)
11761186

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

11811192
def test_index_to_datetime(self):

pandas/tseries/tools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def _convert_listlike(arg, box, format, name=None):
310310
if not isinstance(arg, DatetimeIndex):
311311
return DatetimeIndex(arg, tz='utc' if utc else None)
312312
if utc:
313-
arg = arg.tz_convert(None)
313+
arg = arg.tz_convert(None).tz_localize('UTC')
314314
return arg
315315

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

0 commit comments

Comments
 (0)