Skip to content

Commit 64de074

Browse files
authored
BUG: Retain timezone information in to_datetime if box=False (#22457)
* BUG: Retain timezone information in to_datetime if box=False * fix test and add issue number to whatsnew * add comments
1 parent 0f656f7 commit 64de074

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ without timezone localization. This is inconsistent from parsing the same
239239
datetime string with :class:`Timestamp` which would preserve the UTC
240240
offset in the ``tz`` attribute. Now, :func:`to_datetime` preserves the UTC
241241
offset in the ``tz`` attribute when all the datetime strings have the same
242-
UTC offset (:issue:`17697`, :issue:`11736`)
242+
UTC offset (:issue:`17697`, :issue:`11736`, :issue:`22457`)
243243

244244
*Previous Behavior*:
245245

pandas/core/tools/datetimes.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,25 @@ def _convert_listlike_datetimes(arg, box, format, name=None, tz=None,
275275
yearfirst=yearfirst,
276276
require_iso8601=require_iso8601
277277
)
278-
if tz_parsed is not None and box:
279-
return DatetimeIndex._simple_new(result, name=name,
280-
tz=tz_parsed)
278+
if tz_parsed is not None:
279+
if box:
280+
# We can take a shortcut since the datetime64 numpy array
281+
# is in UTC
282+
return DatetimeIndex._simple_new(result, name=name,
283+
tz=tz_parsed)
284+
else:
285+
# Convert the datetime64 numpy array to an numpy array
286+
# of datetime objects
287+
result = [Timestamp(ts, tz=tz_parsed).to_pydatetime()
288+
for ts in result]
289+
return np.array(result, dtype=object)
281290

282291
if box:
292+
# Ensure we return an Index in all cases where box=True
283293
if is_datetime64_dtype(result):
284294
return DatetimeIndex(result, tz=tz, name=name)
285295
elif is_object_dtype(result):
296+
# e.g. an Index of datetime objects
286297
from pandas import Index
287298
return Index(result, name=name)
288299
return result

pandas/tests/indexes/datetimes/test_tools.py

+11
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,17 @@ def test_iso_8601_strings_with_same_offset(self):
592592
result = DatetimeIndex([ts_str] * 2)
593593
tm.assert_index_equal(result, expected)
594594

595+
def test_iso_8601_strings_same_offset_no_box(self):
596+
# GH 22446
597+
data = ['2018-01-04 09:01:00+09:00', '2018-01-04 09:02:00+09:00']
598+
result = pd.to_datetime(data, box=False)
599+
expected = np.array([
600+
datetime(2018, 1, 4, 9, 1, tzinfo=pytz.FixedOffset(540)),
601+
datetime(2018, 1, 4, 9, 2, tzinfo=pytz.FixedOffset(540))
602+
],
603+
dtype=object)
604+
tm.assert_numpy_array_equal(result, expected)
605+
595606
def test_iso_8601_strings_with_different_offsets(self):
596607
# GH 17697, 11736
597608
ts_strings = ["2015-11-18 15:30:00+05:30",

pandas/tests/test_algos.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,9 @@ def test_datetime64_dtype_array_returned(self):
330330
'2015-01-01T00:00:00.000000000+0000'],
331331
dtype='M8[ns]')
332332

333-
dt_index = pd.to_datetime(['2015-01-03T00:00:00.000000000+0000',
334-
'2015-01-01T00:00:00.000000000+0000',
335-
'2015-01-01T00:00:00.000000000+0000'],
336-
box=False)
333+
dt_index = pd.to_datetime(['2015-01-03T00:00:00.000000000',
334+
'2015-01-01T00:00:00.000000000',
335+
'2015-01-01T00:00:00.000000000'])
337336
result = algos.unique(dt_index)
338337
tm.assert_numpy_array_equal(result, expected)
339338
assert result.dtype == expected.dtype

0 commit comments

Comments
 (0)