Skip to content

Commit 9d3731b

Browse files
committed
BUG: to_datetime not localizing Series when utc=True (pandas-dev#6415)
Modify test case
1 parent 465c59f commit 9d3731b

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ Conversion
260260

261261
- Bug in assignment against datetime-like data with ``int`` may incorrectly converte to datetime-like (:issue:`14145`)
262262
- Bug in assignment against ``int64`` data with ``np.ndarray`` with ``float64`` dtype may keep ``int64`` dtype (:issue:`14001`)
263-
263+
- Bug in :func:`to_datetime` incorrectly localizing dates in `Series` when `utc=True` (:issue:`6415`)
264264

265265
Indexing
266266
^^^^^^^^

pandas/core/tools/datetimes.py

+4
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,10 @@ def _convert_listlike(arg, box, format, name=None, tz=tz):
508508
from pandas import Series
509509
values = _convert_listlike(arg._values, False, format)
510510
result = Series(values, index=arg.index, name=arg.name)
511+
if utc and isinstance(values, np.ndarray):
512+
# If a numpy array is returned from _convert_listlike,
513+
# the resulting Series will be naive.
514+
result = result.dt.tz_localize('UTC')
511515
elif isinstance(arg, (ABCDataFrame, MutableMapping)):
512516
result = _assemble_from_unit_mappings(arg, errors=errors)
513517
elif isinstance(arg, ABCIndexClass):

pandas/tests/indexes/datetimes/test_tools.py

+26
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,32 @@ def test_to_datetime_utc_is_true(self):
270270
expected = pd.DatetimeIndex(data=date_range)
271271
tm.assert_index_equal(result, expected)
272272

273+
# GH 6415: UTC=True with Series
274+
data = ['20100102 121314', '20100102 121315']
275+
result = pd.to_datetime(pd.Series(data),
276+
format='%Y%m%d %H%M%S',
277+
utc=True)
278+
expected_data = [pd.Timestamp('2010-01-02 12:13:14', tz='utc'),
279+
pd.Timestamp('2010-01-02 12:13:15', tz='utc')]
280+
expected = pd.Series(expected_data)
281+
tm.assert_series_equal(result, expected)
282+
283+
# GH 15760 UTC=True with Series
284+
ts = 1.5e18
285+
result = pd.to_datetime(pd.Series([ts]), utc=True)
286+
expected = pd.Series([pd.Timestamp(ts, tz='utc')])
287+
tm.assert_series_equal(result, expected)
288+
289+
test_dates = ['2013-01-01 00:00:00-01:00'] * 10
290+
expected_data = [pd.Timestamp('20130101 01:00:00', tz='utc')] * 10
291+
expected = pd.Series(expected_data)
292+
ser = Series(test_dates)
293+
result = pd.to_datetime(ser, utc=True)
294+
tm.assert_series_equal(result, expected)
295+
ser_naive = Series(test_dates, dtype='datetime64[ns]')
296+
result = pd.to_datetime(ser_naive, utc=True)
297+
tm.assert_series_equal(result, expected)
298+
273299
def test_to_datetime_tz_psycopg2(self):
274300

275301
# xref 8260

pandas/tests/test_multilevel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2130,7 +2130,7 @@ def test_set_index_datetime(self):
21302130
'2011-07-19 08:00:00', '2011-07-19 09:00:00'],
21312131
'value': range(6)})
21322132
df.index = pd.to_datetime(df.pop('datetime'), utc=True)
2133-
df.index = df.index.tz_localize('UTC').tz_convert('US/Pacific')
2133+
df.index = df.index.tz_convert('US/Pacific')
21342134

21352135
expected = pd.DatetimeIndex(['2011-07-19 07:00:00',
21362136
'2011-07-19 08:00:00',

0 commit comments

Comments
 (0)