Skip to content

Commit

Permalink
BUG: Return UTC DateTimeIndex when specified in to_datetime
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gfyoung authored and jreback committed Feb 20, 2016
1 parent 49f99a6 commit e39f63a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
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

0 comments on commit e39f63a

Please sign in to comment.